
After this lesson, you will be able to:
routes.But first, we need to learn variables.
Yes! Regular variables.
x = "this string"
Difference: Here, we’re in the Flask app.
Very specific use cases:
Variables come from:
These aren’t set inside def hello().
hello_variables.py
from flask import Flask
app = Flask(__name__)
my_job_title = "Python pro"
@app.route('/')
def hello():
return "Hello, " + my_job_title
if __name__ == '__main__':
app.run(debug=True)my_website.py, comment out the return render_template("index.html").Instead, have:
You’re never limited to just one .py file!
New Python file: mySecrets.py
How would we print that in our Flask app?
Any ideas?
Your normal Flask app:
from flask import Flask
import mySecrets ## You can import any file!
app = Flask(__name__)
## Call it like a module.
my_name = mySecrets.username
my_password = mySecrets.password
@app.route('/')
def hello():
return "Hello, " + my_name + ", welcome to " + my_password
if __name__ == '__main__':
app.run(debug=True)Why?
py FileNow it’s your turn!
python_variables.py in the same folder as my_website.py.python_variables.py - perhaps some books you like.python_variables into your Flask app, my_website.py.python_variables in your Flask app.Let’s create a .txt file called hi.txt in the same folder where our app lives. We’ll include some Shakespeare poetry.
So are you to my thoughts as food to life,
Or as sweet-seasoned showers are to the ground;
How do you think we get this into our Flask app?
Then, we’ll add a bit in our Flask app:
import os # Note the new import — to be in the file system.
file_path = '.'
# Note the "with"! We don't need "close".
with open(os.path.join(file_path, 'hi.txt')) as f:
the_text = f.read()
@app.route('/text')
def read_txt():
return the_text.txt FileNow it’s your turn!
more_variables.txt in the same folder as my_website.py.more_variables.txt — perhaps what you’d like for breakfast tomorrow.import os so you can find the file.more_variables in your Flask app.What are the three approaches to read in variables to a Flask app?
@app.route('/'), Anyway?We have:
/).def home() if someone goes there.@app.route('/') # When someone goes here...
def home(): # Do this.
return render_template("index.html")http://127.0.0.1:5000/ => render_template("index.html")"
What if we want to go to http://127.0.0.1:5000/sayHi?
In my_website.py, under def home(), add:
Reload the page! Go to http://127.0.0.1:5000/sayHi.
http://127.0.0.1:5000/sayHihttp://127.0.0.1:5000/sayHihttp://127.0.0.1:5000/Catshttp://127.0.0.1:5000/profilesayHi, Cats, /, and profile are endpoints from our main app.
We only need to add:
my_website.py, add a new route to a randnum endpoint.random module? You can use randint(1, 100).str(number).@app.route('/sayHi/<name>')
def hello(name):
return "Hello, " + name + ", your coding skills impress me!"http://localhost:5000/sayHi/Hari => Hello, Hari, your coding skills impress me!"
Try adding route in your Flask app to have:
/timesfour/<number> route that displays the product of an integer in the route multiplied by four.repeat route that takes a string passed into the URL, then displays it four times in a row.Your code should look similar to this:
from flask import Flask, render_template
import mySecrets ## You can import any file!
import python_variables
import os # Note the new import — to be in the file system.
import random
app = Flask(__name__)
## Call it like a module.
my_name = mySecrets.username
my_password = mySecrets.password
student_name = python_variables.student_name
file_path = '.'
with open(os.path.join(file_path, 'more_variables.txt')) as f:
the_text = f.read()
@app.route('/text')
def read_txt():
return the_text
@app.route('/') # When someone goes here...
def home(): # Do this.
return render_template("index.html")
@app.route('/sayHi/<name>')
def hello(name):
return "Hello, " + name + ", your coding skills impress me!"
@app.route('/timesfour/<number>')
def timesfour(number):
return str(int(number) * 4)
@app.route('/repeat/<number>')
def repeat(number):
return number * 4
if __name__ == '__main__':
app.run(debug=True)We covered variables and routing in Flask:
file to read it.@app.route(<endpoint>) is how we make new pages in our app!