Unofficial example
Ask the Flask docs
The Ask this site box, reading flask.palletsprojects.com/en/stable. Type a question and it answers from those pages, with links.
Loading the box…
An unofficial example. Ask this site is not affiliated with or endorsed by the Flask project. The Flask docs belong to their authors and are read here under their BSD-3-Clause licence.
Questions it answered
Real answers from this box.
How do I run the development server?
The recommended way is the
flask runcommand. Use--appto point to your application and--debugto enable debug mode, which starts the server at http://localhost:5000/. Theruncommand replacesFlask.run()in most cases.You can also start the server from Python with
Flask.run(), which takes arguments similar to the CLI options and acceptsdebug=True. Place the call inside anif __name__ == "__main__":block so it does not interfere when a production server imports the application later.- Debug mode (
--debug): Enables the interactive debugger and the reloader by default. The reloader restarts the server when Python code or imported modules change. - Top-level
--debug: The option can also go on the top-levelflaskcommand, soflask --app hello --debug runandflask --app hello run --debugare equivalent. - Error handling on reload: With
flask runand the reloader, the server keeps running after you introduce syntax or initialization errors, and the site shows the debugger for the error. A syntax error already present when you callflask runmakes it fail immediately. WithFlask.run(), the server crashes if there are errors when reloading.
Do not use the development server in production. It is not designed to be efficient, stable, or secure, and deployment options are covered separately.
Answered from Development Server — Command Line, Command Line Interface — Run the Development Server, Development Server — In Code, API — flask.cli.run_command = <Command run>, Development Server — Deferred Errors on Reload, Development Server
- Debug mode (
How do I register a blueprint?
You register a blueprint on the application by calling
app.register_blueprint()with the blueprint object, for exampleapp.register_blueprint(simple_page). In a factory-style app, you import the blueprint inside the factory function and register it just before returning the app.To mount it at a different location, pass a
url_prefixargument, such asapp.register_blueprint(simple_page, url_prefix='/pages'). Other options you can pass when registering include:- url_prefix: the prefix prepended to all URLs defined on the blueprint.
- cli_group: the name of the group the blueprint's CLI commands are nested under, or
Noneto merge them into the application's top level. - name: changes the name the blueprint is registered with, so the same blueprint can be registered multiple times with unique names for
url_for.
You can also register a blueprint more than once, but not every blueprint supports this, since it depends on how the blueprint is implemented. Registering the same blueprint with the same name multiple times is an error. Once an application has been created, you cannot unregister a blueprint without destroying the whole application object.
To nest blueprints, register a child on a parent blueprint with
parent.register_blueprint(child), then register the parent on the application withapp.register_blueprint(parent).Answered from Modular Applications with Blueprints — Registering Blueprints, Blueprints and Views — Create a Blueprint, API — class flask.blueprints.BlueprintSetupState(blueprint, app, options, first_registration), Command Line Interface — Registering Commands with Blueprints, API — class flask.Blueprint(name, import_name, static_folder=None, static_url_path=None, template_folder=None, url_prefix=N… (cont. 2), Modular Applications with Blueprints — Why Blueprints?, Modular Applications with Blueprints — Nesting Blueprints
How do I return JSON from a view?
You can return a dict directly from the view, and Flask serializes it to JSON automatically. A list returned from a view is converted to a JSON response in the same way. Flask does this with the same behavior as the jsonify() function, so all data in the dict or list must be JSON serializable.
To return another JSON type, or to build the response explicitly, use the jsonify() function, which serializes the given arguments and returns a response with the application/json mimetype. Either positional or keyword arguments can be given, not both, and calling it with no arguments serializes None.
Some conditions and limits apply:
- Complex objects such as database models must be converted to valid JSON types first, typically with a serialization library.
- File data should not be returned in a JSON response, because JSON cannot represent binary data directly; serve files from a separate view and include a URL to them in the JSON.
- Indentation is added in debug mode to make output easier to read.
- Key sorting and ASCII escaping are enabled by default in the default provider, and output is compact outside debug mode.
Answered from JavaScript, fetch, and JSON — Return JSON from Views, Quickstart — APIs with JSON, API — flask.json.jsonify(*args, **kwargs), API — class flask.json.provider.DefaultJSONProvider(app)
Add this to your own docs
Paste your docs address to try it, free and without signing up. Then it is one line of code.
DocusaurusGitBookMintlifyWordPressReadMeMkDocsVitePressStarlightSphinxHugoJekyllNext.jsGhostWebflow