생활코딩 Flask Web Framework를 기반으로 간단하게 정리합니다.
server.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hi'
app.run(port=5001)
from flask import Flask
import random
app = Flask(__name__)
@app.route('/')
def index():
return str(random.random()) # 기본적으로 문자열 값을 반환해야 함.
app.run(port=5001, debug=True)
@app.route('/read/<id>')
def read(id):
print(id)
return 'read : ' + id
topics = [
{ 'id' : 1, 'title' : 'html', 'body' : 'html is ...'},
{ 'id' : 2, 'title' : 'css', 'body' : 'css is ...'},
{ 'id' : 3, 'title' : 'javascript', 'body' : 'javascript is ...'}
]
@app.route('/')
def index():
liTags = ''
for topic in topics:
liTags = liTags + f'<li><a href="/read/{topic["id"]}/">{topic["title"]}</a></li>'
return f'''<!doctype html>
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{liTags}
</ol>
<h2>Welcome</h2>
Hello, Web
</body>
</html>
'''
def template(contents, content):
return f'''<!doctype html>
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{contents}
</ol>
{content}
</body>
</html>
'''
def getContents():
liTags = ''
for topic in topics:
liTags = liTags + f'<li><a href="/read/{topic["id"]}/">{topic["title"]}</a></li>'
return liTags
@app.route('/read/<int:id>/')
def read(id):
title = ''
body = ''
for topic in topics:
if id == topic['id']:
title = topic['title']
body = topic['body']
return template(getContents(), f'<h1>{title}</h1>{body}')
def template(contents, content):
return f'''<!doctype html>
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{contents}
</ol>
{content}
<ul>
<li><a href="/create/">create</li>
</ul>
</body>
</html>
'''
@app.route('/create/', methods = ['GET', 'POST'])
def create():
if request.method == 'GET':
content = '''
<form action="/create/" method="POST">
<p><input type="text" name="title" placeholder="title"></p>
<p><textarea name="body" placeholder="body"></textarea></p>
<p><input type="submit" value="create"></p>
</form>
'''
return template(getContents(), content)
elif request.method == 'POST':
global nextId
title = request.form['title']
body = request.form['body']
newTopic = {'id' : nextId, 'title' : title, 'body' : body}
topics.append(newTopic)
url = '/read/' + str(nextId) + '/'
nextId = nextId + 1
return redirect(url)
@app.route('/update/<int:id>/', methods = ['GET', 'POST'])
def update(id):
if request.method == 'GET':
title = ''
body = ''
for topic in topics:
if id == topic['id']:
title = topic['title']
body = topic['body']
break
content = f'''
<form action="/update/{id}/" method="POST">
<p><input type="text" name="title" placeholder="title" value="{title}"></p>
<p><textarea name="body" placeholder="body">{body}</textarea></p>
<p><input type="submit" value="update"></p>
</form>
'''
return template(getContents(), content)
elif request.method == 'POST':
title = request.form['title']
body = request.form['body']
for topic in topics:
if id == topic['id']:
topic['title'] = title
topic['body'] = body
break
url = '/read/' + str(id) + '/'
return redirect(url)
@app.route('/delete/<int:id>/', methods = ['POST'])
def delete(id):
for topic in topics:
if id == topic['id']:
topics.remove(topic)
break
return redirect('/')