5: Tour Flaskinni
Last updated
Last updated
I can describe the MVT components of Flaskinni.
I can describe the thread's steps as my app starts up.
Remember, you can help add comments and docstrings to Flaskinni and submit pull requests. Even beginners can help contribute to open source projects in surprisingly substantial ways.
The reason why the Application Factory is so important has to do with something called Flask's Application Context. Our app's "context" is what takes the assortment of Python files and modules which make up our app and brings them together so that they see and work with one another.
When your app is serving hundreds or thousands of people at a time, your server will run many instances of your app. You may also run your app in different modes, like a text-only shell. An application factory addresses this need by spitting out instances of app
Flaskinni comes with a main
and an api
blueprint. That'll affect your use of url_for
. So if you wanted to make a link to the homepage, it'd be <a href="{{ url_for('main.index') }}">home</a>
Now that Flaskinni is running in your environment, local or cloud-based, let's take a tour.
This is the whole parent-child thing. The parent is the skeleton of your page. We named it base.html and it has <link>
s to our Google Fonts, CSS files, and sets up nav bars and any other element that’s site-wide. It’s great! Pretty much sets up the whole theme of our site.
To put our pages inside this metaphorical picture frame, we use the command, {% extends "base.html" %}
at the top of every other HTML we put in our templates folder.
The base template extends across all pages, so the website can share HTML code.
We’re going to make a new template that inherits from our base.html so you can experiment with this handy tool. Create a new file in your templates folder called about.html. This page will need hardly any information at all because all the setup is done in the parent. All we need to do is pass some HTML into any block.
Don't want to learn? There is a lazy way out and that's to edit your CSS directly. No judgments.
Add them to your collection and get the <link> we’ll use to embed a connection to Google Fonts.
Replace the other <link> to fonts.google.com that was in your base.html file
Add your CSS code to custom.css, something like:
Launch your server
(make sure you’ve got venv activated) source venv/bin/activate
or on Windows: source venv/Scripts/Activate
(use our flask-script to run the server) flask run
Check out your font changes!
If you want to see the new template file in action, we’ve got to build a route to that page. As of this writing, there are only two locations for routes in Flaskinni, the master __init__.py
file and the views.py
file inside the blog module folder. Since our homepage route is in the first file, we’ll put our about route there, too.
We later passed a variable to the template. We dropped <h1> {{ some_variable }} </h1>
in our about.html
file. Nothing shows up until we change the render_template call to something like this:
Congratulations! You just passed your first variable to a template.
We have cool, color-changing notifications in Flaskinni that rely on Bootstrap classes. So if you want to send a good alert message that something worked, you'd say:
flash("Your message has been sent!", "success")
But if something failed, you'd write:
flash("Your message was destroyed by mistake. Whoops!", "danger")
What other Flash notifications are there? How are the flash notifications being built? Let's look at where they come up in the base.html file, trace back the macro that builds the flash messages and then see what other options come with Flaskinni.
Cookies!
Let's talk about how this blog post is queried from our database:
Check out line #20.
We've got a handy little method that instantiates our app, loads the settings, and straps on all its expansion packs. Let's start our tour of the code where Flask is spawned. From "":
The T part of our MVT all starts with base.html
.
The purpose of the base template is to give all the pages in your app common assets. This such as fonts, CSS files, nav bar. Every page must link to the base file. It is kept in the /templates
folder, as base.html
. The base template is like a picture frame, loading common elements like the nav bar, footer, and your little .
Sass makes CSS easier, even if you have to spend a few minutes here and there refreshing yourself on how to use the Sass tricks and how to compile your Sass down to CSS. I personally use a to manage this automatically.
Pick out a header and a body font at