Last active
April 16, 2019 00:11
-
-
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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you suggest where should we add our DB connection check, logger/sentry initialization?