Skip to content

Instantly share code, notes, and snippets.

@cedbeu
Last active April 16, 2019 00:11
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cedbeu/5596125 to your computer and use it in GitHub Desktop.
Save cedbeu/5596125 to your computer and use it in GitHub Desktop.
Minimal Flask application - Basic package design pattern Create a structure like this: `./run.py` `./webapp/__init__.py` `./webapp/views.py`
#!/usr/bin/python
# coding: utf-8
""" Filename: __init__.py
Purpose: This file is required to structure the web service as a
package, to be able to define routes in multiple modules.
This is the most basic design pattern for multiple files
Flask apps: http://flask.pocoo.org/docs/patterns/packages/
Requirements:
Author: Cédric Beuzit
"""
from flask import Flask
app = Flask(__name__)
# application wide global variables and config parameters must be defined here
# (not in `run.py`) for being able to import them in the beginning of the
# views files but we can perfectly imagine a smarter config procedure
app.config['HELLO_WORLD'] = 'Hello Flask!'
# The views modules that contain the application's routes are imported here
# Importing views modules MUST BE in the end of the file to avoid problems
# related to circular imports http://flask.pocoo.org/docs/patterns/packages
import webapp.views
#!/usr/bin/python
# coding: utf-8
""" Filename: run.py
Purpose: This file runs the Flask application service
Requirements: Flask
Author: Cédric Beuzit
"""
from webapp import app
if __name__ == '__main__':
app.run(debug=True)
#!/usr/bin/python
# coding: utf-8
""" Filename: views.py
Purpose: This file is one of the views file that can contain the
routes for the application
Requirements:
Author: Cédric Beuzit
"""
from webapp import app
# importing application wide parameters and global variables that have been
# defined in __init__.py
message = app.config['HELLO_WORLD']
@app.route('/')
def webapp():
return message
@ajay2611
Copy link

Can you suggest where should we add our DB connection check, logger/sentry initialization?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment