[Flask] Help with Blueprints ...

Ben Duncan linux4ms at gmail.com
Thu Oct 31 11:00:04 EDT 2019


Oh yeah, I almost forgot - My config file:
import os
from datetime import timedelta

SECRET_KEY = os.urandom(64)
PERMANENT_SESSION_LIFETIME =  timedelta(minutes=15)
# Per user config:
# app.config['PERMANENT_SESSION_LIFETIME'] =  timedelta(minutes=15)

SQLALCHEMY_DATABASE_URI = 'postgres://
flask:flask at 10.13.70.47:7103/ac03303_live'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_POOL_SIZE = 10
SQLALCHEMY_ECHO = False

# Debugging testing
DEBUG = True
DEVELOPMENT = True
TESTING = True
TEMPLATES_AUTO_RELOAD = True

# SERVER_NAME = ''

# YOUR defined variables go here
APPLICATION = 'MECS APPLICATION'
AUTHOR = 'Ben Duncan'
# Session management
SESSION_TYPE = 'filesystem'
SESSION_FILE_DIR = 'flask_session'
SESSION_COOKIE_PATH = '/'
SESSION_KEY_PREFIX = 'flsk'

*Ben Duncan*
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division


On Thu, Oct 31, 2019 at 9:46 AM Ben Duncan <linux4ms at gmail.com> wrote:

> Ok, I followed to the letter :
>
> https://hackersandslackers.com/managing-user-session-variables-with-flask-sessions-and-redis/
> (but not the redis)
> And from Miguels Fine book - Flask Mega Tutorial
> and neither 'session' nor 'g' is in the application context. I'm beginning
> to wonder if there is a a bug somewhere ...
>
>
> *Ben Duncan*
> DBA / Chief Software Architect
> Mississippi State Supreme Court
> Electronic Filing Division
>
>
> On Thu, Oct 31, 2019 at 9:32 AM Ben Duncan <linux4ms at gmail.com> wrote:
>
>> Ok, tried:
>> import sys
>>
>> from flask import Flask, g, session
>> from flask_sqlalchemy import SQLAlchemy
>> from flask_session import Session
>> from flask.templating import Environment
>>
>> flask_session = 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 = Flask('mars')
>>
>>     app.config.from_pyfile('config.cfg')
>>     flask_session.init_app(app)
>>     session = {'MASTERBLASTER': "Some really stupid stuff"}
>>
>> and I get that dreaded 'out of context' on the view ....
>>
>> As always thanks ...
>>
>> *Ben Duncan*
>> DBA / Chief Software Architect
>> Mississippi State Supreme Court
>> Electronic Filing Division
>>
>>
>> On Thu, Oct 31, 2019 at 8:19 AM Josh Stephens <bsdtux at gmail.com> wrote:
>>
>>> 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/d089eddf/attachment.html>


More information about the Flask mailing list