[Flask] Blueprints Question ...

Paul Götze paul.christoph.goetze at gmail.com
Thu Oct 24 17:21:27 EDT 2019


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 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/20191024/5d07b657/attachment.html>


More information about the Flask mailing list