Coding in Python 24 - Serving a Web Page

From LearnLinux.tv Wiki
Revision as of 09:08, 23 March 2019 by Jay (talk | contribs) (Created page with "== Overview == In the 24th video of the Coding in Python series, you'll see how you can serve an HTML page with Python, using Flask. {| class="wikitable" |- ! Relevant Links...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview

In the 24th video of the Coding in Python series, you'll see how you can serve an HTML page with Python, using Flask.

Relevant Links
Original Video


Commands Used in this Video

Setting up the environment

virtualenv -p /usr/bin/python3 my-project
cd my-project
source bin/activate
pip install Flask

Running a simple website

#!/usr/bin/env python3

from flask import Flask
app = Flask(__name__)


@app.route("/")
def message():
    return "Python is awesome!"


app.run()
==== Flask with debug option ====
#!/usr/bin/env python3

from flask import Flask
app = Flask(__name__)


@app.route("/")
def message():
    return "Python is awesome!"


app.run(debug=True)

Use an html page

mkdir templates
nano templates/site.html

site.html:

<!DOCTYPE html>
<html>
<body>

Python is awesome!

This site was created from Python, using Flask

</body>
</html>


#!/usr/bin/env python3

from flask import Flask, render_template

app = Flask(__name__)


@app.route("/")
def html_page():
    return render_template('site.html')


app.run(debug=True)