Flask Static Files
The focus of this article is to explain the mechanism behind Flask static files and also highlight how you can take advantage of this functionality in your own web applications.
If you’re a developer building a Flask web application, you may need to include static files such as images, stylesheets, and JavaScript files.
Flask offers a convenient way to serve these static files, allowing you to enhance the user experience and improve the performance of your web application.
Create Flask Static Files
As a developer building a web application with Flask, you’ll likely need to include static files like CSS or JavaScript to ensure proper rendering of your web pages.
In most cases, the web server is configured to handle serving these files, but during development, they can be accessed from the static folder within the package or module of your Flask application through the /static URL.
To create URLs for static files in Flask, you’ll use the ‘static’ endpoint.
For example, if you have a script.js file that contains a JavaScript function triggered by an OnClick event in the form.html file, you can create a URL for it like this:
Example: 
Below is the HTML script for form.html:
Example: 
To create a JavaScript file in Flask, you can simply create a new file with the .js extension and save it in the static directory.
For example, if you want to create a JavaScript file called script.js, you would save it in the static directory like this:
your_project/
├── static/
│ └── script.js
├── templates/
│ └── form.html
└── app.py
Include greeting() function in the script.js file:
Example: 
To make a CSS file in Flask, you can create a new file with the .css extension and keep it in the static folder.
For example, if you want to create a CSS file called style.css, you should save it in the static folder like this:
your_project/
├── static/
│ └── script.js
│ └── style.css
├── templates/
│ └── form.html
└── app.py
Apply external css on Mr.Examples button:
Example: 
Example Explanation
- The first line of above code imports Flask framework and render_template function
- The second line creates an instance of the Flask class and stores it in a variable called “app“.
- The “@app.route(‘/’)” decorator links the function called show_site() to the root URL (‘/‘).
- When a user visits the root URL, Flask executes the show_site() function.
- The show_site() function uses the render_template() function to generate an HTML response, which is based on the contents of the “form.html” template file.
- The “name == ‘main‘” condition ensures that the app.run() method is executed only when the script is run directly.
- When the app runs, the Flask development server starts, and the app becomes available on the specified host and port.
- The “debug=True” argument is passed to the app.run() method to enable Flask’s debugging mode, which displays detailed error messages when an error occurs in the application.
Serving Static Files in Production
In production, it’s recommended to serve static files using a dedicated web server or a content delivery network (CDN) for better performance.
Flask provides a convenient way to do this by using the “Flask-Assets” extension, which allows you to bundle and compress your static files and serve them through a CDN or a dedicated server.
Flask Static Files Benefits
Flask’s static files have several advantages, which include:
- Serving static files from the web server rather than the Flask application leads to faster response times as the web server can cache the files and serve them more efficiently.
- Separating static files from dynamic content reduces the workload on the Flask application and web server because static files are typically updated less frequently and require less processing.
- Storing static files in a separate directory makes it easier to organize and manage them. Flask also provides a built-in way to generate URLs for static files, making it easier to reference them in templates and code.
- Flask static files feature includes a built-in mechanism that prevents unauthorized access to static files, ensuring that sensitive data is safeguarded.
- You can experience a better development experience and create more visually appealing web applications by utilizing Flask static files feature. With this feature, you can easily work with web assets such as CSS, JavaScript, and images, making the development process smoother and more efficient.
Conclusion
By utilizing Flask’s feature for static files, you can enjoy numerous benefits that improve the overall performance, security, and maintenance of your web applications. Separating static files from dynamic content reduces the workload on the web server, which leads to faster response times. Organizing and managing static files becomes easier with Flask’s built-in mechanism, ensuring their safety and integrity. Furthermore, working with web assets such as CSS, JavaScript, and images becomes more straightforward, resulting in more visually appealing web applications and a better experience for the developer.