Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Flask Framework Cookbook

You're reading from   Flask Framework Cookbook Over 80 hands-on recipes to help you create small-to-large web applications using Flask

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher
ISBN-13 9781783983407
Length 258 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Shalabh Aggarwal Shalabh Aggarwal
Author Profile Icon Shalabh Aggarwal
Shalabh Aggarwal
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Flask Configurations FREE CHAPTER 2. Templating with Jinja2 3. Data Modeling in Flask 4. Working with Views 5. Webforms with WTForms 6. Authenticating in Flask 7. RESTful API Building 8. Admin Interface for Flask Apps 9. Internationalization and Localization 10. Debugging, Error Handling, and Testing 11. Deployment and Post Deployment 12. Other Tips and Tricks Index

Organization of static files

Organizing static files such as JavaScript, stylesheets, images, and so on efficiently is always a matter of concern for all web frameworks.

How to do it…

Flask recommends a specific way to organize static files in our application:

my_app/
    - app.py
    - config.py
    - __init__.py
    - static/
       - css/
        - js/
        - images/
            - logo.png

While rendering them in templates (say, the logo.png file), we can refer to the static files using the following line of code:

<img src='/static/images/logo.png'>

How it works…

If there exists a folder named static at the application's root level, that is, at the same level as app.py, then Flask will automatically read the contents of the folder without any extra configuration.

There's more…

Alternatively, we can provide a parameter named static_folder to the application object while defining the application in app.py:

app = Flask(__name__, static_folder='/path/to/static/folder')

In the img src path in the How to do it… section, static refers to the value of static_url_path on the application object. This can be modified as follows:

app = Flask(
    __name__, static_url_path='/differentstatic',
    static_folder='/path/to/static/folder'
)

Now, to render the static file, we will use the following:

<img src='/differentstatic/logo.png'>

Note

It is always a good practice to use url_for to create the URLs for static files rather than explicitly define them:

<img src='{{ url_for('static', filename="logo.png") }}'>

We will see more of this in the upcoming chapters.

You have been reading a chapter from
Flask Framework Cookbook
Published in: Nov 2014
Publisher:
ISBN-13: 9781783983407
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime