Created
March 17, 2020 15:19
-
-
Save macloo/a7f71a6be0eead13795a48d91144e7ca to your computer and use it in GitHub Desktop.
Basic Flask app before adding templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
app = Flask(__name__) | |
import csv | |
def convert_to_dict(filename): | |
datafile = open(filename, newline='') | |
my_reader = csv.DictReader(datafile) | |
list_of_dicts = list(my_reader) | |
datafile.close() | |
return list_of_dicts | |
# you will need to get the CSV elsewhere | |
pres_list = convert_to_dict('presidents.csv') | |
@app.route('/') | |
def index(): | |
link1 = '<p><a href="president/1">George Washington</a></p>' | |
link2 = '<p><a href="president/2">John Adams</a></p>' | |
link3 = '<p><a href="president/3">Thomas Jefferson</a></p>' | |
return link1 + link2 + link3 | |
@app.route('/president/<num>') | |
def president(num): | |
pres = f'<h1>Hello, {num}!</h1>' | |
return pres_list[int(num) - 1 ] | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can get the CSV here:
https://github.com/macloo/python-adv-web-apps/blob/master/python_code_examples/flask/presidents/presidents.csv
This file represents the end result of this video:
https://www.youtube.com/watch?v=832svAqCOEU
.