Initialization of the drawing of flags - initialization of some global variables

I'm new to Flask, and I'm going to write a larger application. So far I have divided the functionality into drawings. Now I want to be able to set some global variable from the drawing during initialization (so outside of the request context). Basically, I want to automatically initialize the navigation list for some drawings, so every project should tell my base application how it wants to be named and what its default route is.

My goal is that other people can distribute my application by simply placing their own plan in the plugin application folder. In this case, my application does not know their routes or names. It should automatically study it when loading a specific drawing ...

To explain it in another way: I have a main application containing some routines implemented as drawings. The main application should contain a navigation bar that applies to all auxiliary applications (drawings). How can a file register something in this navigation variable of the main menu (for example, during initialization)?

(I did not find a way to access something like "self.parent" or the application context from the drawing. Do the drawings have something like a constructor?)

+4
source share
2 answers

therefore, each drawing must tell my base application how it wants to be named and what the default route is.

When creating a drawing, you already pass your name in the first parameter:

simple_page = Blueprint('simple_page')

You can also pass url_prefix to the constructor

simple_page = Blueprint('simple_page', url_prefix='/pages')

I have a main application that implements several routines as drawings. The main application should contain a navigation bar referring to all auxiliary applications (drawings)

This is an example in one python module, you have to separate each project in your own module.

from flask import Flask, Blueprint, render_template

# ADMIN
admin = Blueprint('admin', __name__, url_prefix='/admin')

@admin.route('/')
def admin_index():
    return 'Admin module'

@admin.route('/settings')
def settings():
    return 'Admin Settings'


# USER
user = Blueprint('user', __name__, url_prefix='/user')

@user.route('/')
def user_index():
    return 'User module'

@user.route('/profile')
def profile():
    return 'User Profile'


app = Flask(__name__)
app.register_blueprint(admin)
app.register_blueprint(user)

@app.route('/')
def index():
    return render_template('index.html', blueprints=app.blueprints)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, port=7000)

Jinja2. , .

<ul>
  {% for bp_name, bp in blueprints.iteritems() %}
    <li><a href="{{ bp.url_prefix }}">{{ bp.name }}</a></li>
  {% endfor %}
</ul>

, , ; app.blueprints, python , . , , URL .

, , , (, - ...)

, , Blueprint , .

custom.py

from flask import Blueprint

class MyBlueprint(Blueprint):

    bp_data = {}

    def set_data(data):
        # here you can make extra task like ensuring if 
        # a minum group of value were provided for instance
        bp_data = data

admin.py

from .custom import MyBlueprint

admin = MyBlueprint('admin', __name__, url_prefix='/admin')
admin.set_data({'color': '#a569bd', 'enabled': true})

# all the blueprint routes
...

app.py

from admin import admin

app = Flask(__name__)
app.register_blueprint(admin)

@app.route('/')
def index():
    for a, b in app.blueprints:
        print b.bp_data['color']
        print b.bp_data['enabled']
    ...

, Blueprint , , , , ; title, require_auth .. , .

0

, . , .

:

YourApp
|- plugins (will contain all the user plugins)
     |- __init__.py (empty)
     |-plugin1
          |- __init__.py (empty)
          |- loader.py
|- app.py

app.py

from flask import Flask
import os

def plugin_loader(app):
  """ Function for discover new potencial plugins.
  After checking the validity (it means, check if it implements
  'register_as_plugin', load the user defined blueprint"""

  plugins = [x for x in os.listdir('./plugins')
             if os.path.isdir('./plugins/' + x)]
  for plugin in plugins:
    # TODO: Add checking for validation
    module = __import__('plugins.' + str(plugin) + '.loader', fromlist=['register_as_plugin'])

    app = module.register_as_plugin(app)

  return app

# Creates the flask app
app = Flask(__name__)
# Load the plugins as blueprints
app = plugin_loader(app)

print(app.url_map)


@app.route('/')
def root():
  return "Web root!"

app.run()

/plugin 1/loader.py

from flask import Blueprint


plugin1 = Blueprint('plugin1', __name__)

@plugin1.route('/version')
def version():
  return "Plugin 1"

def register_as_plugin(app):
  app.register_blueprint(plugin1, url_prefix='/plugin1')

  return app

, - register_as_plugin loader.py /.

https://docs.python.org/2/library/os.path.html. https://docs.python.org/2/library/functions.html# import.

Python 2.7.6, Flask 0.10.1 Unix.

0

Source: https://habr.com/ru/post/1626689/


All Articles