
This directory is relative (!) to the Blueprint 'auth' directory. Then you tell Flask that there is a directory 'templates' in the Blueprint 'auth' directory. If you specify: auth_blueprint = Blueprint('auth', _name_, template_folder='templates') But how can we access the shared templates? Fortunately we can specify a 'templates' directory when we create a Blueprint. I did not specify a template_folder when creating the Flask app meaning Flask uses the default 'templates' folder. import auth_blueprintĪpp.register_blueprint(auth_blueprint, url_prefix='//auth')įrom import auth_blueprintĪpp.register_blueprint(auth_blueprint, url_prefix='//auth') Changes to template_folders For example, in app_frontend Python files we change the imports of Python files from: # register blueprintsįrom.


Of course we must make changes to the inital imports. In the create_app() of both the app_frontend and app_admin we create the Flask app with the same static folder: flask_static_folder = os.getenv('FLASK_STATIC_FOLDER') The reason is that the static directory is at a different location on the production system. I already have an environment variable that holds the static directory. | | |- class_input_validation.py Changes to the static folder Now it is time to add a shared directory, see below. In a previous post I wrote about using DispatcherMiddleware and presented a basic directory structure. There are also other templates that should be shared, like macros to put forms on a page, macros that put buttons in tables. The templates used by these Blueprints should also be shared. I am also using Blueprints that should be shared, for example the 'auth' Blueprint handling the authentication functions like log in, create account, reset password. For example I wrote classes like MailMessage and FormValidation. Then we have certain classes we wrote ourselves.

Things we want to shareīoth apps use the same database meaning we want to share the models.py file.
#FLASK BLUEPRINT EXAMPLE CODE#
That is true but often there is a lot of code we want to share between these apps. The Flask documents state that the Flask applications in this case are entirely isolated from each other. It uses DispatcherMiddleWare to run the frontend app and the admin app. status_code = 200 assert b "Results found: 0" in response. get ( url_for ( "example.index" )) assert response. override ( github_client_mock ): response = client. Mock ( spec = Github ) github_client_mock. data def test_index_no_results ( client, app ): github_client_mock = mock. data assert b "owner2-avatar-url" in response. data assert b "owner2-login" in response. data assert b "owner1-avatar-url" in response. data assert b "owner1-login" in response. status_code = 200 assert b "Results found: 2" in response. Mock ( login = "owner2-login", html_url = "owner2-url", avatar_url = "owner2-avatar-url", ), get_commits = mock. Mock ( html_url = "repo2-url", name = "repo2-name", owner = mock. unwire () def test_index ( client, app ): github_client_mock = mock. fixture def app (): app = create_app () yield app app. """Tests module.""" from unittest import mock import pytest from github import Github from flask import url_for from.
