Slide 1

Slide 1 text

Fast Flask dev for Big Codebases

Slide 2

Slide 2 text

ssslides

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Abdur-Rahmaan Janhangeer Help people get into OpenSource People hire me to work on Python projects www.compileralchemy.com 6

Slide 7

Slide 7 text

Fav foreign (https://metabob.com) World's most advanced code analysis tool? Fav local (https://oceandba.com) 7

Slide 8

Slide 8 text

Fast Flask dev for Big Codebases 8

Slide 9

Slide 9 text

The Flask Story 9

Slide 10

Slide 10 text

Begin new project 10

Slide 11

Slide 11 text

Code features 11

Slide 12

Slide 12 text

Deploy 12

Slide 13

Slide 13 text

Refine Deploy 13

Slide 14

Slide 14 text

TEST AND PROD ENV DIFFERENT 14

Slide 15

Slide 15 text

USE CONFIGURATION PROFILES 15

Slide 16

Slide 16 text

ADD APP FACTORY 16

Slide 17

Slide 17 text

APP BREAKING 17

Slide 18

Slide 18 text

WRITE TESTS 18

Slide 19

Slide 19 text

CODE DISORGANISED 19

Slide 20

Slide 20 text

ORGANISE CODEBASE 20

Slide 21

Slide 21 text

TESTS ARE BORING 21

Slide 22

Slide 22 text

WRITE FIXTURES, WRITE SCHEMAS 22

Slide 23

Slide 23 text

I HAVE TO START A NEW PROJECT 23

Slide 24

Slide 24 text

HAVE A TEMPLATE 24

Slide 25

Slide 25 text

NOW I HAVE TO USE APIS 25

Slide 26

Slide 26 text

INTEGRATE API-FRAMEWORK 26

Slide 27

Slide 27 text

pulls some more hair 27

Slide 28

Slide 28 text

I WANT TO INTEGRATE CELERY 28

Slide 29

Slide 29 text

INTEGRATE CELERY 29

Slide 30

Slide 30 text

dev left ... 30

Slide 31

Slide 31 text

THE NEW INTERN DOES NOT UNDERSTAND ANYTHING 31

Slide 32

Slide 32 text

WRITE DOCS!!! 32

Slide 33

Slide 33 text

You need something that flies in all weather 33

Slide 34

Slide 34 text

The Application Factory 34

Slide 35

Slide 35 text

def create_app(app_name): app = Flask(app_name) return app 35

Slide 36

Slide 36 text

Configuration Profiles 36

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Tests 39

Slide 40

Slide 40 text

Use Pytest Use class-based testing Define fixtures for common actions e.g. logout/login 40

Slide 41

Slide 41 text

Docs 41

Slide 42

Slide 42 text

Use Sphinx from the start 42

Slide 43

Slide 43 text

Separate Initialisations From The Factory 43

Slide 44

Slide 44 text

Use a file for initialisations Reduces import errors to a max 44

Slide 45

Slide 45 text

app.py init.py 45

Slide 46

Slide 46 text

init.py db = SQLAlchemy() ma = Marshmallow() login_manager = LoginManager() migrate = Migrate() mail = Mail() 46

Slide 47

Slide 47 text

from init import mail def create_app(...): app = ... mail.init_app(app) in other files: from init import db 47

Slide 48

Slide 48 text

Use modules for everything!!! 48

Slide 49

Slide 49 text

app.py module1/ module2/ module3/ 49

Slide 50

Slide 50 text

app.py modules/ module1/ module2/ 50

Slide 51

Slide 51 text

module1/ templates/ static/ test/ models.py forms.py view.py etc etc 51

Slide 52

Slide 52 text

Use flask to simplify your life 52

Slide 53

Slide 53 text

@app.cli.command("jump", short_help="skip something") def jump(): # do something return 0 # cli flask jump 53

Slide 54

Slide 54 text

Automate blueprint registration 54

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

Automate module creation 56

Slide 57

Slide 57 text

# pseudocode def createmodule(name): mkdir(folder) touch(folder/models.py) touch(folder/) ... # cli flask createmodule apple 57

Slide 58

Slide 58 text

Form help yourself 58

Slide 59

Slide 59 text

Use flask-wtf Use wtforms-alchemy class ConfForm(ModelForm): class Meta: model = Conf 59

Slide 60

Slide 60 text

Contain static in module, implement collecstatic 60

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

What for celery? 63

Slide 64

Slide 64 text

app.py init.py celery_app.py modules/ m1/ task.py 64

Slide 65

Slide 65 text

init.py celery = Celery( __name__, broker=BaseConfig.CELERY_BROKER_URL, backend=BaseConfig.CELERY_RESULT_BACKEND, ) 65

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

celery_app.py from app import init_celery celery = init_celery() 67

Slide 68

Slide 68 text

modules/m1/task.py from init import celery @celery.task(name="do something", bind=True) def task_do_something(self): 68

Slide 69

Slide 69 text

What for flask-restx? 69

Slide 70

Slide 70 text

modules/ app.py init.py api_util.py 70

Slide 71

Slide 71 text

init.py api = Api( version="1.0", title="MVP Data", description="Backend Part", authorizations=authorizations, ) 71

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

app.py from init import api from api_util import add_api def create_app(...): app = ... api.init_app(app) add_api(api) 73

Slide 74

Slide 74 text

Migrations to the next level 74

Slide 75

Slide 75 text

# make flask-migrate auto detect models for folder in modules: import(f'modules.{folder}.models.py') 75

Slide 76

Slide 76 text

Modular migration ideas: make flask_migrate or alembic load only specific classes 76

Slide 77

Slide 77 text

Reuse base 77

Slide 78

Slide 78 text

Use cookiecutter cookiecutter Keep modules and init.py empty 78

Slide 79

Slide 79 text

The ongoing conclusion: 79

Slide 80

Slide 80 text

https://github.com/shopyo/shopyo Star it to show support! 80

Slide 81

Slide 81 text

Why not use Django? Because Flask 81

Slide 82

Slide 82 text

82

Slide 83

Slide 83 text

83

Slide 84

Slide 84 text

84

Slide 85

Slide 85 text

85

Slide 86

Slide 86 text

86

Slide 87

Slide 87 text

87

Slide 88

Slide 88 text

And, that's what shopyo is! 88

Slide 89

Slide 89 text

Like it? Appreciate a mail at: arj.python[@]gmail[.]com Means a lot to me! 89