[Flask] Help with Blueprints ...

Josh Stephens bsdtux at gmail.com
Thu Oct 31 09:19:54 EDT 2019


Ben,

  Sorry I just saw this after replying to your first email. Can you try
moving your flask_session = Session() out of the create_app(). Keep the
flask_session.init_app(app) where it is. I am thinking this is probably
where your issue is coming from because flask_session is not at a global
level but in your function and thus I feel like it might be disappearing
after your return app from create_app

Best Regards,
Josh Stephens


On October 30, 2019 at 7:55:35 AM, Ben Duncan (linux4ms at gmail.com) wrote:

I've gotten my blueprint stuff working partially. I'm using the function
style
as defined here :
https://danidee10.github.io/2016/11/20/flask-by-example-8.html

I have a master directory called mars and my blueprints are in a directory
called view under mars.

My mars  application __init__ looks like:
# -------------------------------------------- _root _iinit__.py
import sys
from flask import Flask, g
from flask_sqlalchemy import SQLAlchemy
from flask_session import Session
from flask import session

# Globally accessible libraries
pgdb = SQLAlchemy()
from sqlalchemy import MetaData
from flask_sqlalchemy import SQLAlchemy
metadata = MetaData()


def create_app():
    """Initialize the core application."""
    #app = Flask(__name__, instance_relative_config=False)
    app = Flask(__name__)

    app.config.from_pyfile('config.cfg')
    flask_session = Session()
    flask_session.init_app(app)

    session = {'modified': False }
    session = {'route': None }
    session = {'MASTERBLASTER': "Some really stupid stuff"}

    # initialize Flask extensions
    #pgdb = SQLAlchemy()
    pgdb.app = app
    pgdb.init_app(app)
    pgdb.Model.metadata.reflect(pgdb.engine)
    class office_table(pgdb.Model):
       __table__ = pgdb.Model.metadata.tables['office']

    # Initialize Blueprints
    with app.app_context():
        # Include our Routes
        #from . import routes

        # Register Blueprints
        from .views.home import home
        app.register_blueprint( home )

        from .views.auth import auth
        app.register_blueprint( auth )

        from .views.ar import ar
        app.register_blueprint( ar )

         return app
# -------------- End of Init

My mars wsgi.py looks like this:

#---------------------- Wsgi

# Base wsgi application starter

from mars import create_app
app = create_app()
if __name__ == "__main__":
  app.run()

#-----

And my run script looks like:
#!/bin/sh

#rm -rf flask_session/*
export FLASK_APP=wsgi.py
export FLASK_ENV=development
flask run --host=0.0.0.0 --cert=adhoc

Under views, __iinit__.py is empty and and my home.py looks like
#---- home.py
# mars/views/home
from flask import Blueprint, render_template, request, session
# Insert libraries as needed !!!

home = Blueprint('home', __name__)

@home.route('/<user_url_slug>')
def welcome(user_url_slug):
# Do some stuff
    return render_template('home/welcome.html', session=session)

#--- end of home.
My html template file has this in it:

  <h1>Hello From Template Form !!!</h1>
  <h1>Insert your code here !!!</h1>
  <h1>---- HOME ----</h1>
  <h1>---- 2 HOME ----</h1>
  <h1>session:  {{ session['MASTERBLASTER'] }} </h1>

*My question/issue is* , that I need 'session' found in root __init__.py
and the
pgdb stuff to be available to the views/<bp>.py

Am I setup correctly above? Am I missing something, what do I need to do ?
-- OR --
DO I need to give up and go home 😉 ?


Thanks ...
*Ben Duncan*
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division
_______________________________________________
Flask mailing list
Flask at python.org
https://mail.python.org/mailman/listinfo/flask
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20191031/f676fa33/attachment.html>


More information about the Flask mailing list