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

Fast Flask Dev For Big Codebases

Fast Flask Dev For Big Codebases

Abdur-Rahmaan Janhangeer

November 24, 2022
Tweet

More Decks by Abdur-Rahmaan Janhangeer

Other Decks in Technology

Transcript

  1. 4

  2. Python Mauritius UserGroup (pymug) More info: mscc.mu/python-mauritius-usergroup-pymug/ 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 5
  3. Abdur-Rahmaan Janhangeer Help people get into OpenSource People hire me

    to work on Python projects www.compileralchemy.com 6
  4. 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 37
  5. 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 38
  6. 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) 55
  7. 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 61
  8. 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 62
  9. 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 66
  10. 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") 72
  11. app.py from init import api from api_util import add_api def

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

    import(f'modules.{folder}.models.py') 75
  13. 82

  14. 83

  15. 84

  16. 85

  17. 86

  18. 87