2

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

2 Answers2

1
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!

Will Croxford
  • 457
  • 2
  • 7
  • 21
  • Calling `from app.__init__ import main` inside `app/blueprint/views.py` , is this pythonic? In addition, the `app/__init__.py` is the top directory of my app. –  Oct 23 '17 at 15:50
  • 1
    Actually sorry I just saw you can put import statements in the __init__ file, see: http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html – Will Croxford Oct 23 '17 at 15:57
  • 1
    Ps probably don't * import everything as a rule, see https://stackoverflow.com/questions/17255737/importing-variables-from-another-file comments second answer here for explanation.You have it the wrong way round I think, you don't import things from the init file, into this file is OK though. As this blog explains, if you import into the init file, you then have these imported packages available in that local Python module. – Will Croxford Oct 23 '17 at 16:04
1

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