4,743 questions
840
votes
24
answers
1.1m
views
How to serve static files in Flask
So this is embarrassing. I've got an application that I threw together in Flask and for now it is just serving up a single static HTML page with some links to CSS and JS. And I can't find where in the ...
176
votes
5
answers
96k
views
Are global variables thread-safe in Flask? How do I share data between requests?
In my application, the state of a common object is changed by making requests, and the response depends on the state.
class SomeObj():
def __init__(self, param):
self.param = param
def ...
124
votes
2
answers
402k
views
Sending data from HTML form to a Python script in Flask
I have the code below in my Python script:
def cmd_wui(argv, path_to_tx):
"""Run a web UI."""
from flask import Flask, flash, jsonify, render_template, request
import webbrowser
app = ...
520
votes
13
answers
920k
views
How to get POSTed JSON in Flask?
I'm trying to build a simple API using Flask, in which I now want to read some POSTed JSON. I do the POST with the Postman Chrome extension, and the JSON I POST is simply {"text":"hello ...
80
votes
3
answers
57k
views
Are a WSGI server and HTTP server required to serve a Flask app?
Setting up Flask with uWSGI and Nginx can be difficult. I tried following this DigitalOcean tutorial and still had trouble. Even with buildout scripts it takes time, and I need to write instructions ...
136
votes
2
answers
228k
views
Link to Flask static files with url_for
How do you use url_for in Flask to reference a file in a folder? For example, I have some static files in the static folder, some of which may be in subfolders such as static/bootstrap.
When I try ...
258
votes
19
answers
395k
views
Flask raises TemplateNotFound error even though template file exists
I am trying to render the file home.html. The file exists in my project, but I keep getting jinja2.exceptions.TemplateNotFound: home.html when I try to render it. Why can't Flask find my template?
...
10
votes
3
answers
10k
views
JavaScript raises SyntaxError with data rendered in Jinja template
I am trying to pass data as JSON from a Flask route to a Jinja template rendering JavaScript. I want to iterate over the data using JavaScript. The browser shows SyntaxError: Unexpected token '&'...
47
votes
2
answers
89k
views
How to pass a variable between Flask pages?
Suppose I have following case;
@app.route('/a', methods=['GET'])
def a():
a = numpy.ones([10,10])
...
return render_template(...) # this rendered page has a link to /b
@app.route('/b', methods=...
23
votes
1
answer
21k
views
Reference template variable within Jinja expression
I have a route defined like this:
@app.route('/magic/<filename>')
def moremagic(filename):
pass
And now in a template I want to call that route using url_for() like so:
<h1>you ...
29
votes
3
answers
32k
views
Display data streamed from a Flask view as it updates
I have a view that generates data and streams it in real time. I can't figure out how to send this data to a variable that I can use in my HTML template. My current solution just outputs the data to ...
13
votes
1
answer
18k
views
FastAPI UploadFile is slow compared to Flask
I have created a FastAPI endpoint, as shown below:
@app.post("/report/upload")
def create_upload_files(files: UploadFile = File(...)):
try:
with open(files.filename,'wb+') as wf:
...
121
votes
7
answers
432k
views
How to run a flask application?
I want to know the correct way to start a flask application. The docs show two different commands:
$ flask -a sample run
and
$ python3.4 sample.py
produce the same result and run the application ...
8
votes
2
answers
12k
views
Post values from an HTML form and access them in a Flask view
I have an HTML form that gets posted to a Flask route. However, request.form is empty. If I try to access one of the values by id, I get a 400 error. How do I post values from an HTML form and ...
162
votes
9
answers
273k
views
How can I pass data from Flask to JavaScript in a template?
My app makes a call to an API that returns a dictionary. I want to pass information from this dict to JavaScript in the view. I am using the Google Maps API in the JS, specifically, so I'd like to ...
343
votes
10
answers
484k
views
How to execute raw SQL in Flask-SQLAlchemy app
How do you execute raw SQL in SQLAlchemy?
I have a python web app that runs on flask and interfaces to the database through SQLAlchemy.
I need a way to run the raw SQL. The query involves multiple ...
336
votes
13
answers
334k
views
Get IP address of visitors using Flask for Python
I'm making a website where users can log on and download files, using the Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).
I need to get the IP address of users when they ...
104
votes
1
answer
89k
views
Testing code that requires a Flask app or request context
I am getting working outside of request context when trying to access session in a test. How can I set up a context when I'm testing something that requires one?
import unittest
from flask import ...
168
votes
8
answers
217k
views
Read file data without saving it in Flask
I am writing my first flask application. I am dealing with file uploads, and basically what I want is to read the data/content of the uploaded file without saving it and then print it on the resulting ...
372
votes
11
answers
761k
views
Redirecting to URL in Flask
I'm trying to do the equivalent of Response.redirect as in C# - i.e.: redirect to a specific URL - how do I go about this?
Here is my code:
import os
from flask import Flask
app = Flask(__name__)
@...
140
votes
15
answers
202k
views
jsonify a SQLAlchemy result set in Flask [duplicate]
I'm trying to jsonify a SQLAlchemy result set in Flask/Python.
The Flask mailing list suggested the following method http://librelist.com/browser//flask/2011/2/16/jsonify-sqlalchemy-pagination-...
23
votes
5
answers
25k
views
Flask application traceback doesn't show up in server log
I'm running my Flask application with uWSGI and nginx. There's a 500 error, but the traceback doesn't appear in the browser or the logs. How do I log the traceback from Flask?
uwsgi --http-socket ...
244
votes
5
answers
243k
views
How do I get the different parts of a Flask request's url?
I want to detect if the request came from the localhost:5000 or foo.herokuapp.com host and what path was requested. How do I get this information about a Flask request?
184
votes
20
answers
237k
views
TypeError: ObjectId('') is not JSON serializable
My response back from MongoDB after querying an aggregated function on document using Python, It returns valid response and i can print it but can not return it.
Error:
TypeError: ObjectId('...
104
votes
3
answers
155k
views
How can I add a background thread to flask?
I'm busy writing a small game server to try out flask. The game exposes an API via REST to users. It's easy for users to perform actions and query data, however I'd like to service the "game ...