Skip to content

Instantly share code, notes, and snippets.

@ulgoon
Last active April 10, 2022 07:17
Show Gist options
  • Save ulgoon/d175fecf19dfafcf1a2a8b494dcb184e to your computer and use it in GitHub Desktop.
Save ulgoon/d175fecf19dfafcf1a2a8b494dcb184e to your computer and use it in GitHub Desktop.
flask first
from collections import deque
def enqueue(queue, element):
"""
Add element at last of deque
"""
queue.append(element)
def dequeue(queue):
"""
Remove element from 1st position of deque
"""
print(queue.popleft())
def front(queue):
print(queue[0])
def is_empty(queue):
"""
return True if queue is empty, otherwise return False.
"""
if len(queue)==0:
print(True)
return(True)
else:
print(False)
return(False)
if __name__=='__main__':
queue_1 = deque()
enqueue(queue_1, 1)
enqueue(queue_1, 2)
enqueue(queue_1, 3)
print(queue_1)
dequeue(queue_1)
dequeue(queue_1)
dequeue(queue_1)
print(queue_1)
def push(stack, element):
"""
Add value in to the stack
"""
stack.append(element) # return something
def pop(stack):
"""
Remove last value ever pushed
"""
return print(stack.pop())
def peek(stack):
"""
Print and show value what pops next
"""
return print("peek value: {}".format(stack[-1]))
def is_empty(stack):
"""
Check and print whether stack empty or not.
"""
if len(stack)==0:
print(True)
else:
print(False)
if __name__=='__main__':
# 쉘에서 파일이 실행될 때 아래의 일을 합니다.
# Stacking process
stack_1 = [] # list: collection
push(stack_1, 1)
push(stack_1, 2)
push(stack_1, 3)
is_empty(stack_1)
print(stack_1)
peek(stack_1) #3
pop(stack_1)
pop(stack_1)
pop(stack_1)
is_empty(stack_1)
<!doctype html>
<html>
<h1>Home</h1>
<p>This is home.</p>
</html>
<!-- templates 디렉토리가 꼭 필요합니다(오타 꼭 확인할 것) -->
<!-- templates/index.html -->
from flask import Flask, jsonify, render_template
import requests
app = Flask(__name__)
@app.route('/') #decorator
def index():
return render_template('index.html')
# index.html
#<!doctype html>
# <html>
# <h1>Home</h1>
# <p>This is home.</p>
# </html>
# routing: 사용자의 접속 경로를 지정
# 외부 api 정보 가져오기
@app.route('/posts')
def show_posts():
response = requests.get('https://jsonplaceholder.typicode.com/posts')
to_serve = response.json()
return jsonify(to_serve)
# /todos -> "This is todos"
@app.route('/posts/<int:post_num>/comments')
def show_comments(post_num):
response = requests.get('https://jsonplaceholder.typicode.com/posts/{}/comments'.format(post_num))
to_serve = response.json()
return jsonify(to_serve)
@app.route('/quote/<string:name>')
def show_quote(name=None):
return "This is quote {}".format(name)
# rendering with template
# data
product_list = [
{
'product_id': 10000001,
'product_name': 'shoes',
'price': 12000,
'currency': 'KRW',
},
{
'product_id': 10000002,
'product_name': 'cap',
'price': 25.99,
'currency': 'USD',
},
{
'product_id': 10000003,
'product_name': 'pants',
'price': 40000,
'currency': 'KRW',
},
]
# get all products
@app.route('/products')
def show_product_list():
result={'items':product_list}
return render_template('products.html', items=result)
# get product detail
@app.route('/products/<int:product_id>')
def show_product(product_id=None):
result = next(item for item in product_list if item['product_id']==product_id)
return render_template('product.html',item=result)
if __name__=='__main__':
app.run(host='0.0.0.0', port=4000, debug=True)
# 프로젝트 첫 시작시(한번만) $ pip install flask
# shell에서 app 시작: $ python main.py
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{{ item.product_name }}</title>
</head>
<body>
<h1>{{ item.product_name }}</h1>
<ul>
<li>Product id: {{ item.product_id }}</li>
{% if item.currency == 'KRW' %}
<li>Price: ₩ {{ item.price }}</li>
{% elif item.currency == 'USD' %}
<li>Price: $ {{ item.price }}</li>
{% endif %}
</ul>
<p>{{ item }}</p>
</body>
</html>
<!-- templates/product.html -->
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>Products</title>
</head>
<body>
<h1>Product list</h1>
<ul>
{% for item in items['items'] %}
<li>
<a href="{{ url_for('.show_product', product_id=item.product_id) }}">{{ item['product_name'] }}</a>
</li>
{% endfor %}
</ul>
{% for item in items['items'] %}
<p>{{ item }}</p>
{% endfor %}
</body>
</html>
<!-- templates/products.html -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment