User:Pedro Sá Couto/Prototyping 5th/Flask App/Structuring the App: Difference between revisions
(Created page with "=Structuring the App= ===01 Activate the created Virtual Environment=== <pre style="color: silver; background: black;"> $ source venv/bin/activate </pre> ===02 Run the app==...") |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 48: | Line 48: | ||
===08 Create the entry point for the app to run=== | ===08 Create the entry point for the app to run=== | ||
Rename app.py to run.py | |||
<source lang="python"> | <source lang="python"> | ||
Line 56: | Line 56: | ||
app.run() | app.run() | ||
<source | </source> | ||
===09 Change environment variables=== | ===09 Change environment variables=== | ||
Line 91: | Line 91: | ||
</pre> | </pre> | ||
Add to the bottom | Add to the bottom | ||
<source lang="python"> | <source lang="python"> | ||
from app import admin_views | from app import admin_views | ||
Line 106: | Line 105: | ||
<pre style="color: silver; background: black;"> | <pre style="color: silver; background: black;"> | ||
$ pip3 install -r requirements.txt | $ pip3 install -r requirements.txt | ||
</pre> | |||
<br><br><br><br> | |||
<pre> | |||
Building your first Flask app - Python on the web - Learning Flask series Pt. 2 | |||
https://www.youtube.com/watch?v=-BC3V1CUKpU&t=44s | |||
https://pythonise.com/series/learning-flask/flask-application-structure | |||
</pre> | </pre> |
Latest revision as of 22:29, 31 March 2020
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()
09 Change environment variables
$ cd .. $ export FLASK_APP=run.py $ export FALSK_ENV=development $ flask run --host=0.0.0.0
10 Organize admin_views, login_view, api_view, etc
$ cd app $ touch admin_views.py $ vim admin_views.py
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
Building your first Flask app - Python on the web - Learning Flask series Pt. 2 https://www.youtube.com/watch?v=-BC3V1CUKpU&t=44s https://pythonise.com/series/learning-flask/flask-application-structure