Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Advanced Flask: Recipes For An All-weather Craft

Advanced Flask: Recipes For An All-weather Craft

Abstract: Flask is favoured for prototyping. It is easy to set up and run. However, choosing Flask as your main 'cheval de bataille', be it as a company or individual, requires solid grounding. Flask lets you choose your own ingredients, which lights up the joy of coding but bites if not being careful. This talk covers the standard techniques not to be missed as well as new audacious ones used to help manage BIG codebases.

Abdur-Rahmaan Janhangeer

October 21, 2021
Tweet

More Decks by Abdur-Rahmaan Janhangeer

Other Decks in Programming

Transcript

  1. 3

  2. Python Mauritius UserGroup (pymug) More info: mscc.mu/python-mauritius-usergroup-pymug/ pymug.com Why Where

    codes github.com/pymug share events twitter.com/pymugdotcom ping professionals linkedin.com/company/pymug all info pymug.com tell friends by like facebook.com/pymug 4
  3. class Config: TESTING = False class ProductionConfig(Config): FAV_FLOWER = 'rose'

    class DevelopmentConfig(Config): FAV_FLOWER = 'sunflower' class TestingConfig(Config): FAV_FLOWER = 'moonlight petal' TESTING = True 36
  4. from config import ProductionConfig from config import DevelopmentConfig from config

    import TestingConfig profiles = { 'development': DevelopmentConfig(), 'production': ProductionConfig(), 'testing': TestingConfig() } def create_app(profile): app = Flask(__name__) app.config.from_object(profiles[profile]) return app 37
  5. 1. name each blueprint: foldername_blueprint 2. In app.py # pseudocode

    for folder in modules: blueprint = import(f'modules.{folder}.view.{folder}_blueprint') app.register(blueprint) 54
  6. app.py static/ # served from here in production modules/ m1/

    static/ file.png m2/ static/ file.png collectstatic just collects all assets in all modules and add it under app.py static/ modules/ m1/ file.png m2/ file.png 60
  7. define function serve_asset(module, resource_path) e.g. serve_asset('m1', 'file.png') if mode is

    dev serve from /modules/m1/static/file.png i.e /modules/module_name/m1/static/path/to/file.png if mode is prod serve from /static/modules/m1/file.png (assumes collecstatic run before) i.e /static/modules/module_name/path/to/file.png 61
  8. app.py from init import celery def init_celery(app=None): app = create_app(os.getenv("FLASK_ENV",

    "")) app = app or create_app() celery.conf.update(app.config) class ContextTask(celery.Task): """Make celery tasks work with Flask app context""" def __call__(self, *args, **kwargs): with app.app_context(): return self.run(*args, **kwargs) celery.Task = ContextTask return celery 65
  9. api_util.py # from modules.some.api import some_ns from modules.some2.api import some2_ns

    # ns: namespace from init import api_v1 api_v = api_v1 # /api/v1 def add_api(api): api.add_namespace(some_ns, path=api_v + "/someapi") api.add_namespace(some2_ns, path=api_v + "/someapi2") 71
  10. app.py from init import api from api_util import add_api def

    create_app(...): app = ... api.init_app(app) add_api(api) 72
  11. # make flask-migrate auto detect models for folder in modules:

    import(f'modules.{folder}.models.py') 74