-
Notifications
You must be signed in to change notification settings - Fork 22
/
app.py
78 lines (55 loc) · 2.16 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'''
In this example, we are going to look at creating, serving, and deploying a *very simple* webapp. In the following several lecture, we'll see how to add some interesting interactivity to our app.
# Prerequisites
- You need to have the flask package installed in your PIC16B Anaconda environment.
- You need a Heroku account.
- You need the Heroku command line interface:
- To install, at the command line (for MacOS)
brew tap heroku/brew && brew install heroku
- At the command line, run
conda activate PIC16B
# Local Preview
At the command line:
export FLASK_ENV=development; flask run
# Deployment
*Note*: these notes are written for the version of the app that Phil is using, which is indeed called pic16b-minimal-demo. In order to make your own version, you would need to give the app a different name (because one with this name already exists on the internet now).
Sign up for Heroku, create app called pic16b-minimal-demo
```
heroku login
heroku git:remote -a pic16b-minimal-demo
git add *.
git commit -m'add files for heroku'
git push heroku
```
Then, the website is at
https://pic16b-minimal-demo.herokuapp.com
# Sources
This set of lecture notes is based in part on previous materials developed by [Erin George](https://www.math.ucla.edu/~egeo/) (UCLA Mathematics) and the tutorial [here](https://stackabuse.com/deploying-a-flask-application-to-heroku/).
'''
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/")
# simplest possible approach
# def main():
# return "hi there!"
# slightly less trivial
# def main():
# return render_template("main.html")
# A little fancier
# @app.route("/")
def main():
return render_template("main_better.html")
# getting basic user data
@app.route('/ask/', methods=['POST', 'GET'])
def ask():
if request.method == 'GET':
return render_template('ask.html')
else:
try:
return render_template('ask.html', name=request.form['name'], student=request.form['student'])
except:
return render_template('ask.html')
#
@app.route('/profile/<name>/')
def hello_name(name):
return render_template('profile.html', name=name)