20
loading...
This website collects cookies to deliver better user experience
Note: We are not deploying the app, we're just getting started. If you want to deploy a Flask app to Heroku, check out this article.
venv
.mkdir yourproject
cd yourproject/
python -m venv .venv/
source .venv/bin/activate/
pip install flask
pip freeze > requirements.txt
main.py
and add this to it:from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "<h1>Hello world</h1>"
export FLASK_ENV=development
export FLASK_APP=main
flask run
* Serving Flask app 'main' (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 814-739-543
Hello World
in big bold letters. main.py
file and change it to look like this:# main.py
from flask import Flask, render_template
app = Flask(__name__, template_folder='templates')
@app.route('/')
def index():
return render_template('home.html')
templates/
folder as the main folder for templates, and to use home.html
as the main page. You can rename home.html
to anything you like. Note: There's A LOT to know when it comes to templating in Jinja, but we aren't going to cover very much right now so we can keep this simple.
templates/
and add a child file called home.html
. <h3>Hello from home.html</h3>
main.py
add this import:import datetime # <- Add this line
from flask import Flask, render_template
....
main.py
add these lines:@app.route('/time')
def timetest():
now = datetime.datetime.now()
return render_template('time.html', now=now)
templates/time.html
to your project, and inside of time.html
add:<h3>The time is {{ now }}</h3>
Note: If the time doesn't match your exact time right now, it's because your Flask server thinks it's on a different timezone.
now
with the current date/time. , now=now
to the render_template()
function. This adds "context" to your template.{{ now }}
as a dynamic value.