Post on 04-Jan-2022
PYTHON & FLASKအပ င း(၁)
Flask IntroductionBy Kaung Htat Nyunt
Flask Web App နမနာ 1
မာတ ကာ◦Flask_Introduction ဆ တ Python Project
တစ ခ တည ဆဆာက ပါမည
◦app.py file တည ဆဆာက ပါမည
◦app.py file နင index.html တ ခ တ ဆက အလပ လပ ပ
◦app.py file, index.html နင base.html
ခ တ ဆက အလပ လပ ပ တ က ရင းြပားမားမည
Flask Web App နမနာ 2
PyCharm software က ဖင ပါ။PyCharm က အ ောကက Terminal မ PycharmProjects>ဖဖစအေ ပပါမ ။
ပထမဆ း Flask_Introduction နာမညန project directory လပပါမ။
(venv) C:\Users\kaung\PycharmProjects>
Flask Web App နမနာ 3
Flask_Introductionနာမညန project director လပရန
File > New Project က ကလစပါ။
Flask Web App နမနာ 4
C:\Users\kaung\PycharmProjects\Flask_Introduction
Location: C:\Users\kaung\PycharmProjects\Flask_Introduction\venv
Base Interpreter C:\Users\kaung\AppData\Local\Programs\Python\Python38-32\Python.exe
အပ ေါကက project location New
environment location နင Base Interpreter path တ က သတ ထာား ေါ.
Flask Web App နမနာ 5
Successfully installed message and Flask installation
Flask Web App နမနာ 6
Virtual Environment Successful မြြစ ရင python ဆတမ run လ မရပါဘး
pip install FlaskTerminal မာ
Flask install မလ ရပသားရင install လ ပ ား ေါလ မ မည။ Install
လ ြး ားရင Requirement already satisfied ပ မာ ေါ။
App.py
Flask_Introduction
က right click ပ ေါက ေါ
New >
File >
Python File က ပရ ား ေါ။
Flask Web App နမနာ 7
Flask Web App နမနာ 8
app.py
app.py
from flask import Flask, render_templateapp = Flask(__name__)
@app.route('/')def index():
return "Hello, World"
if __name__ == '__main__':app.run(debug= True)
Flask Web App နမနာ 9
app.py ဖ ငက run ရန
right click >Run ‘app’
သ မဟတ
Ctrl+Shift+F10
Flask Web App နမနာ 10
ပထမအဆင အအော ငမမငသ ောားပါပပြ။
ဒတ ယအဆင အအနမဖ င app.py file နင index.html တ ခ တဆကအလပလပပ က ဆကပါမည။
static folder နင templates folder နစခက မပ ြလပ ပါမည ။
Flask Web App နမနာ 12
static folder နင templates folder နစခက မပြလပရန
Flask_Introduction ဆ တ project name အအပေါတ င right click အခါကပါ။
New >Directory က click ပါ။
New Directory တ င static folder နင templates folderနာမညထည ပါ။
Flask Web App နမနာ 13
index.html ဖ င မပြလပရန
templates folder အအပေါတ ငright clickအခါကပါ။
New >HTML File က click ပါ။
HTML File က အရ ားပပြားindex.html နာမညထည ပါ။
Flask Web App နမနာ 14
index.html<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8"><title>Title</title><h1> Python and Machine Learning for Myanmar Engineers"
Facebook Page. </h1></head><body><p>This is an example paragraph.</p></body></html>
Flask Web App နမနာ 15
index.html နင app.py တ ခ တရန app.py ဖ ငမ ကဒ (၁) အ ကောငား မပငပါမည ။
from flask import Flask, render_templateapp = Flask(__name__)
@app.route('/')def index():
return render_template("index.html")
if __name__ == '__main__':app.run(debug= True)
Flask Web App နမနာ 16
rander_template
Index.html ဖ င နင ခ တရန
Index.html က လညား လပပပြားပပြား app.py က လညား
မပငပပြားပပြဆ ရင run လ ရပါပပြ။
အအပ ေါကလ အပေါတယဆ ရငအတော ဒတ ယအဆင
အအော ငမမငသ ောားပါပပ ြ။Flask Web App နမနာ 17
တတ ယအဆင အဆနတ base.html ြ င index.html ြ င နင app.py ြ င ဆတမက ခ တ ဆက မာပါ ◦ base.html ြ င နင index.html ြ င တ က templates folder ဆအာက မာရ ရပါမည
◦အခ base.html က create လပ ပါမည
◦ base.html ြ င က အဆြခခ အြြစ ထားပပ းဆတာ တြခားြ င ဆတမကဆြာ ြပခ င တာဆတမ ားမားထည မာပါ အခ ဥပမာမာ index.html ြ င ထ ကဆြာ ြပခ င တာဆတမ ားမားထည မာပါ
◦ app.py ထ က return render_template("index.html") ကဒ က run တ င းindex.html ဆ ားမားမာပါ index.html ကလည း base.html ြ င ထ မာရ တာဆတမက အဆြခခ ထားပပ း ားြ င ထ က ဆြာ ြပခ င တာ(display လပ ခ င တာ) ဆတမက web brower ဆပေါမာ ဆြာ ြပမာပါ
Flask Web App နမနာ 18
base.html
Flask Web App နမနာ 19
<!DOCTYPE html><html lang="en"><head>
<title> Python and Machine Learning for Myanmar Engineers" Facebook Page </title></head>
<body>{% block head%} {% endblock%}<p>Hello "Python and Machine Learning for Myanmar Engineers" Facebook Page</p>
{% block paragraph%} {% endblock%}</body>
</html>
base.html ဖ ငနင Index.html ဖ င
Flask Web App နမနာ 20
<body>{% block head%} {% endblock%}<p>Hello "Python and Machine Learning for Myanmar Engineers" Facebook Page</p>
{% block paragraph%} {% endblock%}
{% block head%} {% endblock%}{% block paragraph%} {% endblock%}
index.html ဖ ငမာစာားသာားမ ာား ဝငလာ ေါမည။
index.html ဖ င က base.html ဖ ငနင ခ တရန မပငရမည။
Flask Web App နမနာ 21
{% extends 'base.html' %}
{% block head%}<h1> Python and Machine Learning for Myanmar Engineers" Facebook Page. This is from block head</h1>{% endblock%}
{% block paragraph%}<p>This is an example paragraph. This is from block paragraph.</p>{% endblock%}
<h1> Python and Machine Learning for Myanmar Engineers" Facebook Page. This is from block head</h1> သညbase.html ဖ င၏ {% block head%} {% endblock%} အကကာားသ ဝငပရာကသ ာားလ မ မည။ထ အတ <p>This is an example paragraph. This is from block paragraph.</p> လညားbase.html ဖ င၏ {% block paragraph%} {% endblock%} အကကာားသ ဝငပရာကသ ာားလ မ မည။
base.html ြ င လည း ထည ပပ းပပ index.html ြ င က လည း
ြပင ပပ းဆ ရင app.py ြ င က Run လ ရပါပပ
Flask Web App နမနာ 22
အခလ web brower မာ ြမင ရတယ ဆ ရင တတ ယအဆင ဆအာင ြမင ပါပပ
ားင ဟာ python န web app တစ ခ စတင ဘ ဆြခတစ လမ း စမ ပါပပ
- End -