forked from NikhilSinghVIT/brew_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial.py
More file actions
83 lines (57 loc) · 2.5 KB
/
material.py
File metadata and controls
83 lines (57 loc) · 2.5 KB
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
79
80
81
#SLIDE16 and SLIDE19----------------------------------------------
#Printing Hello World with Flask.
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/',methods=['GET','POST']) #----methods syntax is for slide 8------
def index():
return "<h1>Hello World!</h1>"#-------<h1> is a HTML header tag,you can return a normal string as well---------
#SLIDE17----------------------------------------------------------
#Intro to Routes.
#Making another route to properly understand the concept of routes.
@app.route('/home') #--------home route
def home():
return "<h1>You are on the home page </h1>"
@app.route('/json') #optional
def json():
return jsonify({'key1':'value1','key2':'value2'})
#SLIDE21----------------------------------------------------------
#Route Variables
@app.route('/variable',methods=['GET','POST'],defaults={'name':'User'})
@app.route('/variable/<string:name>',methods=['GET','POST'])
def variable(name):
return '<h1>Hello {},you are on the variables page'.format(name)
#SLIDE22-----------------------------------------------------------
#Requesting Query String
@app.route('/query')
def query():
print(request.args)
nm = request.args.get('name')
loc = request.args.get('location')
return '<h1> Hello {} ,you are from {}'.format(nm,loc)
#SLIDE23------------------------------------------------------------
@app.route('/theform',methods=['POST','GET'])
def form():
return '''<form method="POST" action="/process">
Name<input type="text" name="name">
<br>
Location<input type="text" name="location">
<br>
<input type="submit" value="Submit">
</form>'''
@app.route('/process',methods=['POST','GET'])
def process():
nm = request.form.get('name')
loc = request.form.get('location')
return "<h1>Hello {}, from {}, you have successfully submitted the form </h1>".format(nm,loc)
#return redirect(url_for('home', name=name, location=location)) for the redirect, url_for slide
#SLIDE24------------------------------------------------------------
#requesting json data
@app.route('/processjson', methods=['POST'])
def processjson():
data = request.get_json()
name = data['name']
location = data['location']
randomlist = data['randomlist']
return jsonify({'result' : 'Success!', 'name' : name, 'location' : location, 'randomkeyinlist' : randomlist[1]})
if __name__ == "__main__":
app.run(debug=True)#port is for deployment puropose...