Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@macloo
Created March 17, 2020 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macloo/a7f71a6be0eead13795a48d91144e7ca to your computer and use it in GitHub Desktop.
Save macloo/a7f71a6be0eead13795a48d91144e7ca to your computer and use it in GitHub Desktop.
Basic Flask app before adding templates
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)
@macloo
Copy link
Author

macloo commented Mar 17, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment