Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Python န Web Development လပမယဆ စ ပ လလ သငတ Framework တထက တစခ ဖစတ

flask အ က င က Example code တန ရင ပ ပ သ မ ပ .. ​သည နရ မ တစခ ပ ခ ငတ က FrameWork


ဆတ ကပ .. FrameWork ဆတ သည Web Server တ resource တ API တ အတကလအပတ package
တ libraries တ component တ ပ င စပပ ဝင နတ ပ ဖစပ တယ.. Python မ Web Development
အတကသ ငတ frame work တထကမ န မည က တ စခက တ Django န Flask ပ ဖစပ တယ။ Django
အတကက တ ရ ပ ထ ဖ ပ တယ။ အ ကကပ လငက န ဝင ရ က လ လ ကညလရပ တယ

http://crossnetmm.com/?p=1066

ခ ရ မယ အ က င အရ န ပ တသကတ Flask အ က င က ဆကလကက ရ အ င.. Flask သည Django


လ ​full-stack web framework မ
​ ဟတပ Lightweight ဖစတ Framework တစခပ Lightweight ဆတ က
Django လ အ ပညအစ လလမလလ tools, libraries တ ပ နတ မ မဟတပ.. လတ ရ third party lib, module
တ ထပထညရတ .. သမဟတ ကယတင ရ ပ ထညရတ မ ပ .. အတ တ lightweight က ဘ က င လဆ တ
simple ဖစတယ.. ပ ပ ပ ပ (little to no dependencies to external libraries) ဖစတယ ပ ဗ …

ထ စအတင Hello, world ထတမယ web server လ တစခ စ ရ ကညက ရ အ င န .. ပထမဆ


folder/directory တ အရင ဆ ကရပ မယ။ ကယလပမယ web server ရ န မညအတင hello ( က ကတ န မည)
ဆတ folder လ ဆ ကလကပ မယ။ ပ တ သ အ ကမ static န templates ဆတ folder စခ
ထပ ဆ ကလကပ တယ.. ဒ စခက တ န မညက အအတင ပ ရပ မယ standard ဖစ အ င တ အ ငလပ ။

$ mkdir ​-​p hello_flask​/​{templates,static}

$ tree hello_flask/
hello_flask/
|-- static
`-- templates

အ န ကမ တ hello ဆတ Folder လ အ ကမ ပ notepad န ဖစ ဖစ IDE တစခခန ဖစ ဖစ


hello.py ( က ကတ န မည) ဆတ python code တ ရ မယ file လ က ထပ ဆ ကပ ။ ဆ ကထ တ hello.py
ထမ အ ကပ ကတ လ တက ထညလကပ ။ အကယလ flask မရ သ ရင pip3 install flask ဆ ပ pip
ကအသ ပ ပ flask က install လပပ ။
ပထမဆ FLASK (class name) ကflask (package name) က န import လပလကပ တယ ပ တ
object instance တစခဖနတ ဖ app = Flask(__name__) က ရ ပ တယ။ Flask consturactor သည
argument တစခလပ တယ.. Application package န name က ထည ပ လရပ တယ.. Common use အ နန
__name__​ ကထညက ပ တယ။

Creating Routes - route ကသ ပ တ URL န view Function က binding လပပ မယ .. view


function ဆတ က user က သကဆငရ url က reqest လပတ အခ မ response လပ ပ မယ က ငပ python
မ တ route ဆတ Decorator ကသ ပ တယ.. @app.route( )​ မ url က ထညထ ပ ရပ မယ
Syntax က တ အ ကပ အတင ပ ဖစပ တယ။
app.route(rule, options)

● The rule parameter represents URL binding with the function.


● The options is a list of parameters to be forwarded to the underlying Rule object.

ခက န တ တရ Code မ ဆရင url နရ မ ‘/’ လ က တ ရမ မ ပ အ က င လ တ web server ရ


homepage က ကယစ ပ ပ တ hello_world() ဆတ view function လ န သ ပ င နပ တယ။
အအတက က င homepage ကဝငလကရင Hello World ဆတ စ သ လ က မငရမ ပ ဖစပ တယ။

@app.route () decorator ကမသ ပ ​app.add_url_rule() ကလ သ လရပ တယ။ For


Example:

def index():
pass
app.add_url_rule('/', 'index', index)

Starting the server - server က စဖအတက flask object ရ run method (​app.run() )​ က ခ ပ မယ
.. ဒ နရ မ တစခရင ပခ ငတ က တက ကလမ မ ခ ပ ​if __name__ == ​"__main__"​: က
ထညရခ င အ က င ကပ .. ဘ လလဆ တ hello.py သည တစ ခ က န import လပထ တ မဟတပ main
program အ နန run နလ ဆတ က စစတ ပ .. Run နတယဆမ ​app.run() က ခ ပ server က run
စပ တယ။
Running on ​http://127.0.0.1:5000/ လ default အတင ပ နပ လမမယ Web Browser က န
လမ ပ တ localhost:5000 (IP:port) လ က ပ enter ခ ကလကရင flask server ရ homepage က
သ ပ တယ homepage န bind ထ တ view funtion လ က response လပတ မ Hello Word ဆ ပ
မငရတ ပ ။

run() method ရ syntax က တ အ ကပ အတင ပ ဖစပ တယ။ port တ ဘ တ ပ င ခ ငလ


ရပ တယ.. မ ပ င ရင တ default IP, port န server က run ပ တယ

app.run(host, port, debug, options)

Sr.No. Parameters & Description


1 host
Hostname to listen on. Defaults to 127.0.0.1 (localhost). Set to ‘0.0.0.0’ to have
server available externally
2 port
Defaults to 5000
3 debug
Defaults to false. If set to true, provides a debug information
4 options
To be forwarded to underlying Werkzeug server.

Happy learning at home stay during Covid-19…… ^_^

Thanks & Best Regards,


Khant Phyo
Core Network Team Leader @ Ericsson Myanmar

You might also like