user8386722
user8386722

Reputation:

Python, Flask: Importing functions and variables inside blueprint

How to import a function and variable from app/__init__.py and app/blueprint/__init__.py , respectively, inside app/blueprint/views.py ?

app/__init__.py

def main():
    <..>

app/blueprint/__init__.py

from flask import Blueprint
blueprint = Blueprint('blueprint', __name__, template_folder='templates')

app/blueprint/views.py

import blueprint
import main

Upvotes: 2

Views: 3327

Answers (2)

user8386722
user8386722

Reputation:

I read the blog suggested by Will Croxford, and here's the solution to my problem:

app/blueprint/views.py

from app import main
from app.blueprint import blueprint

Upvotes: 1

Will Croxford
Will Croxford

Reputation: 477

from app.__init__ import *
from app.blueprint.__init__ import *

should import all the functions and variables from both files.

However, though I don't think init file is supposed to be used for this.

Below examples of Flask Blueprints I used my project, learnt structure from Udemy tutorial, I think the idea is generally the init files are used to make a Python directory into a package so you can import stuff within it. You'd probable better create new files with the functions (less often variables) you want to import, maybe experts will confirm, but I think generally you leave Python init files blank unless you really know what you're doing.

from flask import Flask, render_template
from Source.common.database import Database

from Source.models.users.views import user_blueprint
from Source.models.street_lists.views import street_list_blueprint
# from Source.models.street_reports.views import street_report_blueprint

__author__ = "Will Croxford, with some base structure elements based on Github: jslvtr, \
from a different tutorial web application for online price scraping"

app = Flask(__name__)
app.config.from_object('Source.config')
app.secret_key = "123"

app.register_blueprint(user_blueprint, url_prefix="/users")
app.register_blueprint(street_list_blueprint, url_prefix="/streetlists")
# app.register_blueprint(street_report_blueprint, url_prefix="/streetreports")


@app.before_first_request
def init_db():
    Database.initialize()


@app.route('/')
def home():
    return render_template('home.jinja2')


@app.route('/about_popup.jinja2')
def info_popup():
    return render_template('about_popup.jinja2')

Flask Views file example:

# In this model, views.py files are the Flask Blueprint for this object.
# ie they describe what HTTP API endpoints are associated to objects of this class.

from flask import Blueprint, render_template, request, redirect, url_for

from Source.models.street_lists.street_list import StreetList

__author__ = 'jslvtr'


street_list_blueprint = Blueprint('street_lists', __name__)


@street_list_blueprint.route('/')
def index():
    prop_query = StreetList.get_from_mongo(streetpart="bum")
    return render_template('street_lists/street_list.jinja2', stores=prop_query)

You can look at pocoo.org flask doc examples, and search other SO questions for Flask blueprint template examples I think. Good luck!

Upvotes: 1

Related Questions