User:Pedro Sá Couto/Prototyping 5th/Flask App/Structuring the App
< User:Pedro Sá Couto | Prototyping 5th
Revision as of 21:22, 31 March 2020 by Pedro Sá Couto (talk | contribs) (→11 Import changes to init)
Structuring the App
01 Activate the created Virtual Environment
$ source venv/bin/activate
02 Run the app
$ flask run --host=0.0.0.0
03 Create app directory
$ mkdir app
04 Go to the app directory
$ cd app
05 Create two new files
$ touch __init__.py views.py
06 __init__.py file
from flask import Flask
app = Flask(__name__)
from app import views
07 views.py file
from app import app
@app.route("/")def index():
return "Hello world"
@app.route("/about")def about():
return "All about Flask"
08 Create the entry point for the app to run
Rename app.py to run.py
from app import app
if __name__ == "__main__":
app.run()
<source lang="python">
===09 Change environment variables===
<pre style="color: silver; background: black;">
$ cd ..
$ export FLASK_APP=run.py
$ export FALSK_ENV=development
$ flask run --host=0.0.0.0
</pre>
===10 Organize admin_views, login_view, api_view, etc===
<pre style="color: silver; background: black;">
$ cd app
$ touch admin_views.py
$ vim admin_views.py
</pre>
<source lang="python">
from app import app
@app.route("/admin/dashboard")
def admin_dashboard():
return "Admin Dashboard"
@app.route("/admin/profile")
def admin_profile():
return "Admin profile"
11 Import changes to init
$ vim __init__.py
Add to the bottom
from app import admin_views
12 Create requirements.txt file
$ pip3 freeze > requirements.txt
13 To install all requirements
$ pip3 install -r requirements.txt