User:Pedro Sá Couto/Prototyping 5th/Flask App/Serving HTML files

From XPUB & Lens-Based wiki
< User:Pedro Sá Couto‎ | Prototyping 5th
Revision as of 21:42, 31 March 2020 by Pedro Sá Couto (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Serving HTML file

01 Activate the created Virtual Environment

$ source venv/bin/activate

02 Run the app

$ flask run --host=0.0.0.0

03 Create a new directory

$ cd app

04 Create templates directory

$ mkdir templates
$ cd template

05 Create a html file

$ touch index.html

06 Render the HTML template

$ vim views.py
from app import app

from falsk import render_template

@app.route("/")
def index():
    return render_template("index.html")
$ cd ..
$ flask run --host=0.0.0.0

07 Structure Templates

public - Will contain all of the HTML files we want to serve from views.py
admin - Will contain any HTML files we'll serve from our admin routes in admin_views.py

$ mkdir public admin

08 Move index.html to public

$ mv index.html /public

09 Create dashboard.html in admin

$ cd admin
$ touch dashboard.html

10 Change the directory path in admin_views.py

$ vim admin_views.py
from flask import render_template

@app.route("/admin/dashboard")
def admin_dashboard():
    return render_template("admin/dashboard.html")

11 Change the directory path in views.py

$ vim views.py
@app.route("/")def index():
    return render_template("public/index.html")

12 Deploy into prodcution

$ cd ..
$ export FLASK_APP=run.py
$ cd app
$ export FLASK_ENV=production
$ FLASK_ENV=production
$ flask run --host=0.0.0.0





Building your first Flask app - Python on the web - Learning Flask series Pt. 3
https://www.youtube.com/watch?v=QrK50lIwgbk
https://pythonise.com/series/learning-flask/rendering-html-files-with-flask