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

From XPUB & Lens-Based wiki
(Created page with "=Serving HTML file= ===01 Activate the created Virtual Environment=== <pre style="color: silver; background: black;"> $ source venv/bin/activate </pre> ===02 Run the app===...")
 
No edit summary
Line 66: Line 66:


===10 Change the directory path in admin_views.py===
===10 Change the directory path in admin_views.py===
<pre style="color: silver; background: black;">
$ vim admin_views.py
</pre>


<source lang="python">
<source lang="python">
Line 76: Line 80:


===08 Change the directory path in views.py===
===08 Change the directory path in views.py===
<pre style="color: silver; background: black;">
$ vim views.py
</pre>
<source lang="python">
<source lang="python">
@app.route("/")def index():
@app.route("/")def index():

Revision as of 22:41, 31 March 2020

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")

08 Change the directory path in views.py

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

08 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