Skip to content

Commit 8b7dc87

Browse files
Add files via upload
0 parents  commit 8b7dc87

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

server.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from flask import Flask, request, redirect
2+
3+
app = Flask(__name__)
4+
5+
nextId=4
6+
topics = [
7+
{'id':1, 'title':'HTML', 'body': "I think html is awesome..."},
8+
{'id':2, 'title':'CSS', 'body': "I think css is awesome..."},
9+
{'id':3, 'title':'JS', 'body': "I think js is awesome..."}
10+
]
11+
# Template
12+
def template(contents, content, id=None):
13+
contextUI= ''
14+
if id!=None:
15+
contextUI=f'''
16+
<li><a href="/update/{id}/">Update</a></li>
17+
<li><form action="/delete/{id}/" method="POST"><input type="submit" value="delete"></li>
18+
'''
19+
return f'''<!doctype html>
20+
<html>
21+
<head>
22+
<title>My Flask Website</title>
23+
</head>
24+
<body>
25+
<h1><a href="/">Homepage</a></h1>
26+
<ol>
27+
{contents}
28+
</ol>
29+
{content}
30+
<ul>
31+
<li><a href="/create/">Create</a></li>
32+
{contextUI}
33+
</ul>
34+
</body>
35+
</html>
36+
'''
37+
def getContents():
38+
liTags=''
39+
for topic in topics:
40+
liTags = liTags+ f'<li><a href="/read/{topic["id"]}/">{topic["title"]}</a></li>'
41+
return liTags
42+
# Root
43+
@app.route('/')
44+
def index():
45+
return template(getContents(), '<h2>Welcome!</h2>Hello world!')
46+
# Read
47+
@app.route('/read/<int:id>/')
48+
def read(id):
49+
title=''
50+
body=''
51+
for topic in topics:
52+
if id == topic['id']:
53+
title = topic['title']
54+
body = topic['body']
55+
break
56+
return template(getContents(), f'<h2>{title}</h2>{body}', id)
57+
# Create
58+
@app.route('/create/', methods=['GET', 'POST'])
59+
def create():
60+
if(request.method == 'GET'):
61+
content='''
62+
<form action="/create/" method="POST">
63+
<p><input type="text" name="title" placeholder="title"></p>
64+
<p><textarea name="body" placeholder="body"></textarea></p>
65+
<p><input type="submit" value="create"></p>
66+
</form>
67+
'''
68+
return template(getContents(), content)
69+
elif request.method == 'POST':
70+
global nextId
71+
title=request.form['title']
72+
body=request.form['body']
73+
newTopic={'id': nextId, 'title':title, 'body':body}
74+
topics.append(newTopic)
75+
url ='/read/'+str(nextId)+'/'
76+
nextId=nextId+1
77+
return redirect(url)
78+
# Update
79+
@app.route('/update/<int:id>/', methods=['GET', 'POST'])
80+
def update(id):
81+
if(request.method == 'GET'):
82+
title=''
83+
body=''
84+
for topic in topics:
85+
if id==topic['id']:
86+
title=topic['title']
87+
body=topic['body']
88+
break
89+
content=f'''
90+
<form action="/update/{id}/" method="POST">
91+
<p><input type="text" name="title" placeholder="title" value={title}></p>
92+
<p><textarea name="body" placeholder="body">{body}</textarea></p>
93+
<p><input type="submit" value="update"></p>
94+
</form>
95+
'''
96+
return template(getContents(), content)
97+
elif request.method == 'POST':
98+
global nextId
99+
title=request.form['title']
100+
body=request.form['body']
101+
for topic in topics:
102+
if id==topic['id']:
103+
topic['title'] = title
104+
topic['body'] = body
105+
break
106+
url ='/read/'+str(id)+'/'
107+
return redirect(url)
108+
# Delete
109+
@app.route('/delete/<int:id>/', methods=['POST'])
110+
def delete(id):
111+
for topic in topics:
112+
if id==topic['id']:
113+
topics.remove(topic)
114+
break
115+
return redirect("/")
116+
117+
app.run(debug=True)

0 commit comments

Comments
 (0)