Giter Club home page Giter Club logo

flask_blueprint_model's Introduction

Flask Blueprints

This repo ties together much of what has been demonstrated in the other Flask demonstrations, such as Flask templates, Flask forms, and Flask models. Here however, it is all structured and tied together using blueprints - making the app easier to maintain and expand.

App structure

Screenshot 2022-05-12 at 09 31 21

Key parts

app.py is now just the entry point to the app, and all code related to making the app is placed in a project directory. Because we no longer configure everything at top level, Flask will look for an __init__.py file inside project directory. We'll place all the configurations for our app there. We then have a clubs directory, which holds the 'clubs' blueprint, and a core directory for the 'core' blueprint.

In clubs/views.py

Import your required methods from flask, including the Blueprint contstructor:

from flask import render_template, url_for, redirect, Blueprint

Import your configured database (when you look in a directory, like 'project', Flask will look for the `init.py file):

from project import db

Then import any models and forms you may have in this blueprint directory:

from project.clubs.models import Club
from project.clubs.forms import CreateClub

Fianlly, define your blueprint (and if you have a template folder in your blueprint as we have in this example, tell flask where to find it with 'template_folder='):

clubs = Blueprint('clubs', __name__, template_folder='templates')

Now you use @clubs.route() instead of @app.route() in this blueprint. Do the same for the core blueprint, or any other your define.

In __init__.py:

Import and register the blueprints to the app:

from project.core.views import core
from project.clubs.views import clubs

app.register_blueprint(core)
app.register_blueprint(clubs)

In app.py:

Import the app so that you can run it:

from project import app

And that's it! Now you have a fully modularised app. In most demos and tutorials, the templates and models are kept outside the blueprints, but I thought it could be useful to see them fully encapsulated, so that they truly are mini-apps that extend your app, rather than have some of their code in the blueprint, and some out.

Setup

If you have not installed Python3, please do.

First create and activate some form of environment to store your dependencies. I like Conda:

$ conda create -n myenv python=3.7

$ conda activate myenv

Or just use Pythons built in environments:

$ python3 -m venv venv

$ . venv/bin/activate

Then install Flask, Flask-SQLAlchemy, Flask-Migrate, and also WTForms (because we added a form to update the database).

$ pip install Flask Flask-WTF Flask-SQLAlchemy Flask-Migrate

Run the app

$ flask run

You should now be able to see the output in your browser window (at http://127.0.0.1:5000)

Resulting pages

index.html

Screenshot 2022-05-12 at 18 12 32

create_club.html

Screenshot 2022-05-12 at 18 12 43

clubs.html

Screenshot 2022-05-12 at 18 13 00

flask_blueprint_model's People

Contributors

narnav avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.