Web applications often require static files, such as javascript files or CSS files that support Web display.
Typically, you configure the Web server and it provides you with this. But during development Flask development, Python parses all web requests.
To solve this, these files are places in the static folder, which will be available in the application’s /static
.
Related course: Python Flask: Create Web Apps with Flask
Static files
Where to place static files
The URL of the special endpoint static
is used to generate a static file. In your programs directory, create a new directory named static.
In this directory you can place images, javascript files, css files and many other files that don’t need a Python backend.
Example
In the following example, the javascript function defined in the hello.js is called on the OnClick event of the HTML button in index.html, which is rendered on the “/“ URL of the Flask application.
1 | from flask import Flask, render_template |
Then index.html
1 | <html> |
Add a javascript file, hello.js
1 | function sayHello() { |
Related course: Python Flask: Create Web Apps with Flask