Flask
2022. 10. 13. 23:55ㆍAI/프로그래머스 AI 코스
Flask
폴더 구조
Flask
├── README.md
├── application.py # 서버 실행 파일
├── database.csv # 데이터 보관
├── database.py # import 모듈
├── static # flask 필수 폴더
│ └── img # flask 필수 폴더
│ └── 1.jpeg
└── templates # flask 필수 폴더
기본 코드
application = Flask(__name__) # 객체 생성
@application.route("/") #"/" 경로로 request 될때 실행
def hello():
return render_template("index.html") # index.html 파일 렌더링
@application.route("/home") #"/home" 경로로 request 될때 실행
def home():
return redirect(url_for('hello')) # hello 함수의 url로 redirect
if __name__ == "__main__":
application.run(host='0.0.0.0', debug = True) # debug 모드로 변경사항 발생시 서버 자동 restart
DB 연결 - Pymysql
@app.route('/menus/DB', methods = ['GET'])
def get_DB():
conn = pymysql.connect(user = 'root', # connect 생성
password = '1234',
host = '127.0.0.1',
database = 'menu',
auth_plugin_map = 'mysql_native_password')
cursor = conn.cursor() # cursor 생성
"""
.cursor() 는 tuple 형태로 return
.DictCursor() 는 Dict 형태로 return
"""
query = ('SELECT * FROM menus') # query 생성
cursor.excute(query) # 데이터 조회
DB_menus = []
for (id, name, price) in cursor:
print(f'{id} {name} {price}')
DB_menus.append({'id': id, 'name': name , 'Price': price})
cursor.close() # 사용후 close
conn.close() # 사용후 close
return jsonify({'menu' : menu})
https://yurimkoo.github.io/python/2019/09/14/connect-db-with-python.html
HTML
form 만들기
http://www.tcpschool.com/html/html_input_forms
<form action="처리할페이지주소" method="get|post">
<input type="text" name="search"> # input type을 선택
</form>
<form action = "/photo" method = "get">
<input placeholder = "위치 입력" required name = "location"/><br>
<input type = "checkbox" name = "clean" value = "clean"/>
<textarea name = "built" placeholder = "빌트인 항목" cols = "45" rows = "4" required></textarea><br>
<button>사진 이어서 입력하기</button>
</form>
파일 업로드
<form method="post" enctype="multipart/form-data" action ="{{ url_for('upload_done') }}">
<input type = "file" name = "file" />
<input type = "submit" />
<a href = 'url'> # link
<img src = 'file_path.png' alt="이미지가 없을때 대체문자열"> # image
내부 코드 입력
{{variable_name}} # 변수 사용
{% code ~~ %} # 코드 사용
{% if ~~%} {%endif%} # if문
{% for a in abc %} {%endfor%} # for문
<img src = {{url_for("static", filename = "filename")}}> # 이미지 출력, static 폴더안의 파일 사용
모바일용 화면 크기 조절
<meta name="viewport" content="width=device-width, initial-scale=1">
'AI > 프로그래머스 AI 코스' 카테고리의 다른 글
Django (0) | 2022.10.24 |
---|---|
Git 요약 (0) | 2022.10.04 |
MySQL (0) | 2022.09.28 |
Selenium (0) | 2022.09.28 |
BeautifulSoup4 (0) | 2022.09.27 |