Flask

Sites Using Flask

Initializing an app

Default directory structure

Flask Blueprints

Debug (watch) mode

Debug mode is also watch mode, meaning that it keeps automatically reloading the site after every save. It also provides detailed outputs in case of an error pinpointing exactly where the error happened and possible reasons as to why it happened. Debug mode is highly recommended during active development. You can either pass it as an argument right after initializing the app:

app = Flask(__name__)
app.debug = True

or pass it as an argument when the app is run:

if __name__ == '__main__':
app.run(debug=True)

Defining custom ‘static’ folder:

I like to call the folder that keeps my web assets ‘assets’ instead of static. For me static represents the build of the site. To set a custom folder (name), you can pass an argument when initializing the app, like:

app = Flask(__name__,static_folder="assets")

and then in templates:

<script src="{{ url_for('static', filename='js/file.js') }}"</script>

Referencing static files:

In the templates, you would use url_for() to get static files.

{{ url_for('static', filename='css/main.css') }}

Note that by default the location of the static folder is ‘/static’. You can change it however by passing custom arguments when initializing the app. In any case, the url_for() will get the defined/default location of the static folder and look for the file in there.

Extensions

  • [Generating static websites (Frozen-Flask)]()
  • [Merge, minify and compile your code (Flask-Assets)]()

Snippets

Examples

Adam Greenhall - Flask Sass Coffee template Digital Ocean - How to structure large Flask applications

Resources