[Flask] Blueprints Question ...

Ben Duncan linux4ms at gmail.com
Fri Oct 25 08:45:33 EDT 2019


Thanks all - Especially Paul ...

Now, I'm getting the following error:
RuntimeError

RuntimeError: Working outside of request context. This typically means that
you attempted to use functionality that needed an active HTTP request.
Consult the documentation on testing for information about how to avoid
this problem.
Traceback *(most recent call last)*

   - File "/data/flask/applications/myproject/wtform.py", line *63*, in
   <module>

   from setup.setup import setup

   - File "/data/flask/applications/myproject/setup/setup.py", line *7*, in
   <module>

   print "SETUP: Session what is our route? :", session['route']

   - File
   "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/local.py",
   line *378*, in <lambda>

   __getitem__ = lambda x, i: x._get_current_object()[i]

   - File
   "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/local.py",
   line *307*, in _get_current_object

   return self.__local()

   - File
   "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/flask/globals.py",
   line *38*, in _lookup_req_object

   raise RuntimeError(_request_ctx_err_msg)


RuntimeError: Working outside of request context. This typically means that
you attempted to use functionality that needed an active HTTP request.
Consult the documentation on testing for information about how to avoid
this problem.


My main process, wtform.py looks like this :
.... (all the necessary imports for flask and wtforms and sqlalchemy) ....
......
app = Flask(__name__)
app.config.from_pyfile('config.cfg')

flask_session = Session()
flask_session.init_app(app)

session = {'modified': False }
session = {'route': None }

# import blueprinted applications here:
from setup.setup import setup

# register blueprinted applications here:
app.register_blueprint(setup, url_prefix='/setup')
.........

The directory 'setup' contains:
   __init__.py             (empty)
  setup.py                 (the module)

and setup.py has this:
# setup - /setup/setup
from flask import g, Blueprint, render_template, abort
from flask import session

setup = Blueprint('setup', __name__)

print "SETUP: Session what is our route? :", session['route']

# @setup.route('/<user_url_slug>')
@setup.route('/')
def dummy():"from setup.setup" to before the "
    # Do some stuff
    return render_template('setup/dummy.html')

So, what AM I doing wrong ?

I tried moving the "app = Flask(__name__)" in wtform.py and it made no
difference

*Thanks for all the help !!!! No Really ....*

*(FWIW: **If I can't get flask configured the way I need, they are going to
implement this accounting system in*
*a 1990's era home brew Perl system ...)*

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


On Thu, Oct 24, 2019 at 4:22 PM Paul Götze <paul.christoph.goetze at gmail.com>
wrote:

> Hi Ben,
>
> in general you should only need to import the functions or classes or
> modules you are actually using in the code of the respective module (e.g.
> your view Python file).
> There might be an exception to this if you want to provide a certain
> function/class/module from a special place, so that it can be imported from
> there. E.g. suppose you have a file (i.e. a module) abc.py were you defined
> a function do_abc() and a file xyz.py were there is a function do_xyz(). If
> you would have a main.py and wanted to import both do_abc() and do_xyz()
> from xyz, then you would need to import do_abc in xyz.py:
>
> # in xyz.py:
> from .abc import do_abc()
>
> def do_xyz():
>     pass
>
> and then you could write:
>
> # in main.py:
> from .xyz import do_xyz, do_abc
>
> and use both. Otherwise you would need to import them separately:
>
> # in main.py:
> from .abc import do_abc
> from .xyz import do_xyz
>
> Maybe try if sth. breaks if you do not include all the imports. Python
> will complain about missing imports. If nothing breaks, then it’s fine :)
> Whether you use the functional or divisional structure should not matter.
>
> (If you are using PyCharm for development, then there is the
> Strg+Shift+Alt+L command that auto-formats your code. You can also
> optimize/remove not needed imports with this. But I guess a code linter in
> your editor would also tell you which imports are not used, by printing
> them gray or the like.)
>
> I hope this helps?
> Cheers, Paul
>
>
> Am 24.10.19 um 16:28 schrieb Ben Duncan:
>
> I'm following some blueprints how to and have a question of configuration.
>
> I'm using the functional method as outlined here:
> http://exploreflask.com/en/latest/blueprints.html
>
> The question is:
> In my Top level Application folder that runs the whole thing , I have
> am using  a lot of import libraries:
>
>
> -----------------------------------------------------------------------------------------------------------------------
> from flask import Flask, render_template, request, redirect, url_for,
> flash, make_response, escape, g
> from flask import get_flashed_messages
> from flask_session import Session
> from flask.templating import Environment
> from flask_sqlalchemy import SQLAlchemy
>
> metadata = MetaData()
> from flask import session
>
> # Flask_wtf from: https://flask-wtf.readthedocs.io/en/stable/
> from flask_wtf import FlaskForm
>
> # CSRF Protection ....
> from flask_wtf.csrf import CSRFProtect
> from flask_wtf.csrf import CSRFError
>
> from wtforms import Form, BooleanField, StringField, PasswordField,
> validators
> from wtforms import TextField, TextAreaField, SubmitField, RadioField,
> SelectField
> from wtforms import DecimalField, BooleanField, IntegerField, FloatField
> from wtforms import DateField, DateTimeField
> from wtforms.validators import *
> from wtforms.widgets import TextArea
>
> ------------------------------------------------------------------------------------------------------------------
> In the blueprinted view of: newapp.py I only have :
>
> from flask import Blueprint, render_template
>
> *The question is:*
>
> *Do I need to include the imports I have in the top level in the
> blueprinted form ?*
>
> Thanks ...
> *Ben Duncan*
> DBA / Chief Software Architect
> Mississippi State Supreme Court
> Electronic Filing Division
>
> _______________________________________________
> Flask mailing listFlask at python.orghttps://mail.python.org/mailman/listinfo/flask
>
> _______________________________________________
> 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/20191025/3ee15034/attachment-0001.html>


More information about the Flask mailing list