#ãã®èšäºã®å¯Ÿè±¡è
ãšæŽ»çšæ¹æ³
###1.ãåå¿è
ã®æ¹
ãã®èšäºã¯ããPythonåå¿è
ããWebã¢ããªåå¿è
ããFlaskåå¿è
ããHTMLåå¿è
ãã®ããããã«åœãŠã¯ãŸãæ¹ããã®äžã§ããéå»ã«âåå¿è
åãâã§æ«æããããšã®ããæ¹ãäž»ãªå¯Ÿè±¡ãšããŠããŸãã
ç§ãæ©ãã ç®æãšãã®åå ãå
šãŠæžããŠããã®ã§ãäžå¿
èŠãªæã¯é©å®é£ã°ãã€ã€ããããã€ããžã§äœãç¥ããªããã ãªããšéŒ»ã§ç¬ããªããèªãã§ãã ããã
ã¡ãªã¿ã«ãç¹ã«èŠããã ã®ã¯ãã¹é¢ä¿ã§ãã
###2. ãåå¿è
ã«æå°ããŠããæ¹
åå¿è
ãã©ãã§ã€ãŸããã®ããåå¿è
ã¯ã©ãããåéããããã®ãçãåå¿è
ã®çæ
ã«ã€ããŠç解ããŠé ããã幞ãã§ãã
###3. ãåå¿è
ã«æ¬æ
ããŠããçç·Žè
ã®æ¹
æ¬åœã®åå¿è
ãšããã®ãã©ããããã®ãããã®èšäºãéããŠä»äžåºŠæãåºããŠé ããã幞ãã§ãã
#ä»åäœæããWebã¢ããª
äœæããWebã¢ããªã®æ©èœã¯å€§ããåããŠä»¥äžã®ïŒã€ã§ãã
- ãã¡ã€ã«ã®ã¢ããããŒã
- Pythonãçšãããã¡ã€ã«ã®åŠç
- åŠçåŸã®ããŒã¿ãšç»åã®è¡šç€º
- åŠçåŸã®ããŒã¿ã®ããŠã³ããŒã
å
·äœçã«ã¯ã15ãã€äœã£ãŠããŸã£ãTã·ã£ããšããŒãããã°ãåžæè
ã«æãæœéžã¢ããªã«ãªããŸãã
åå è
ã®ååãšåžæã®ååãå
¥åãããšãæœéžçµæã衚瀺ãä¿åãããä»çµã¿ã§ãã
æ¬èšäºã¯ä»¥äžã®é çªã§é²ããŠãããŸããäžå¿ èŠãªç®æã¯é©å®é£ã°ããŠãèªã¿ãã ããã
0.ãéçºåã®äžæºåãç°å¢æ§ç¯ããä»®æ³ç°å¢ã¯ãªããŠããªããšããªãã
1.ãWebã¢ããªã®åºæ¬çãªæ§é ããURLã«é ŒããŸããã
2.ããšããããç»å衚瀺ããŠã¿ãããããããŠããã¹ã£ãŠãšãã§ããªãé¢åãããïŒ
3.ããã¡ã€ã«ã®ã¢ããããŒãããããããããŠããã¹ã£ãŠãšãã§ããªãé¢åãããïŒ
4.ã@ã®å€åŽã«å€æ°ãé£ããŠãããsessionã¯ç¥ã ãäžèœã§ã¯ãªãã£ãããã
5.ãPythonã§ã®æœéžåŠçãããããäŸå€ãã¿ãŒã³ãæå€ãšå€ã
6.ãé¢æ°ãå€ããé£ããŠããããããããããŠããã¹ã£ãŠãšãã§ããªãé¢åãããïŒ
7.ãçµæã®åºåãããããããããcsvãexcelããPandasã®ãããã§æå€ãšç°¡å
8.ãèŠãç®ãæŽãããããããããããã§ç»å ŽHTLM
#0.ãéçºåã®äžæºåãç°å¢æ§ç¯ã
ãªããšãªãã¢ãã³ã³ããã€ã³ã¹ããŒã«ããã¯ãããã®ã®ãä»®æ³ç°å¢ãšããã®ã¯ãªãã ãæããã ã£ãã®ã§ãæ¥ããããªãã調ã¹ãããæŸçœ®ããŠããŸããã
ä»®æ³ç°å¢ãšããã®ã¯ã䜿ãæšãŠã®ç°å¢ãã®ãããªãã®ã§ãæ倧ã®é
åã¯ãã®æ°è»œãã«ãããŸãã
ã²ãšãã³ä»®æ³ç°å¢ãäœãã°ãä»ã®ããšã¯äœãæ°ã«ãã奜ããªã©ã€ãã©ãªã®å¥œããªããŒãžã§ã³ãå
¥ããŸããããšãå¯èœã§ããããããããšã§ãCã®éçºã®ããã«ããã®ã©ã€ãã©ãªã®ããŒãžã§ã³ãæ°ããããããæ¢åã®BãšDã®æ¹ã§äžå
·åãããããšãããããªç
©ãããã€ãã³ããã解æŸãããŸãããŸããäœãåé¡ãèµ·ãã£ãæã«ã¯ãã®ä»®æ³ç°å¢ããšåãæšãŠãŠæ°ããäœããªããããšããããšãå¯èœã§ããã¢ãã³ã³ãããã€ã³ã¹ããŒã«ãããŠããã°äœãæ¹ã¯ãšãŠãç°¡åãªã®ã§ããã£ãšèŠãŠãããŸãããã詳ããããšã¯ä»¥äžã®åèèšäºã«èŒã£ãŠããŸãã
åèã«ããèšäºâ ãåå¿è åããAnacondaã§ä»®æ³ç°å¢ãäœã£ãŠã¿ã
###ä»®æ³ç°å¢ãäœã
ã¿ãŒããã«ãéããç°å¢åã決ãã以äžã®ããã«å
¥åããŸãã
ïŒãå
¥ããŠãšã©ãŒãåºãããïŒãæ¶ããŠã¿ãŠäžããã
$ conda create -n ç°å¢å
å€ãã®å Žåãåæã«pythonãã€ã³ã¹ããŒã«ãããã®ã§ä»¥äžã®ããã«ããŸãã
$ conda create -n ç°å¢å python
ãŸãã¯
$ conda create -n ç°å¢å python=ããŒãžã§ã³
ãŸãããã®æ®µéã§ã¢ãã³ã³ãã«ãããã¹ãŠã®ã©ã€ãã©ãªãã€ã³ã¹ããŒã«ãããå Žåã¯ã
$ conda create -n ç°å¢å anaconda
ããã§ä»®æ³ç°å¢ã¯å®æã§ãã
###ä»®æ³ç°å¢ã䜿ãã»ã©ã€ãã©ãªãçªã£èŸŒã¿ãŸãã
ãŸããäœã£ãç°å¢ãèµ·åããŸãã
MacOSã®ãšã
$ source activate ç°å¢å
Windowsã®ãšã
C:(çç¥)>activate ç°å¢å
ããã§å®è¡ç°å¢ãæå®ããä»®æ³ç°å¢ã«å€ãããŸããïŒ
次ã¯äœ¿çšããã©ã€ãã©ãªã®ã€ã³ã¹ããŒã«ã§ããããããããžã§ç°¡åã§ãã
åºæ¬çã«ã¯ã以äžã®äžè¡ã§æžã¿ãŸããå人çã«ã¯ãè¬ã®pipãšè·é¢ã眮ããããªããšã«ããªãæåããŸããã
$ conda install ã©ã€ãã©ãªå
æã åå¿è ã䜿ãåºæ¬çãªã©ã€ãã©ãªãªãããã§åé¡ãªãã®ã§ããããããcondaã察å¿ããŠããªãå Žåã¯å ã»ã©ã®åèèšäºã®æ¹ã§å¯ŸåŠæ³ã«è§ŠããŠãã ãã£ãŠããã®ã§ããã¡ãã«é£ãã§ã¿ãŠäžããã
#1.ãWebã¢ããªã®åºæ¬çãªæ§é
ä»åã¯FlaskãçšããŠWebã¢ããªãäœæããŸãã
ãURLã«é£ãã ãHello worldãåºãŠããWebã¢ããªããäœããåºæ¬çãªæ§é ã説æããŠãããŸãã
ã²ãšãŸãã¯ãhtmlãšpythonã®ãã¡ã€ã«ãURLã§é¢é£ä»ããŠããããšããããšãåããã°OKã§ãã
åèã«ããŸãã£ãèšäºâ Webã¢ããªåå¿è ã®Flaskãã¥ãŒããªã¢ã«
##ãã£ã¬ã¯ããªæ§æ
(any directory)
âapp/
âãâtemplates/
âãâstatic/
âãâapp.py
ârun.py
åºæ¬çã«ã¯ãããªæãã§ããïŒ/ã¯ãã©ã«ããæå³ããŠããŸããïŒ
å®è¡ããã®ã¯run.pyã§ãã¢ããªãå®éã«åããä»çµã¿éšåã¯app.py
templatesãã©ã«ãã¯htmlãã¡ã€ã«ãå
¥ããç®±ãstaticãã©ã«ãã¯ç»åçã®ãã¡ã€ã«ãå
¥ããç®±ã§ãã
ãªããany directory以å€ã®ãã©ã«ãåã¯æ±ºãŸã£ãŠããã®ã§ãäžåäžå¥å€ããŠã¯ãªããŸãããç§ã¯templatesãåæ°åœ¢ã«ããŠããããã§ïŒæéè¬ã®ãšã©ãŒã«èŠãã¿ãŸããã
##1-1. HTMLã䜿ããHello WorldãŸã§ïŒ
ãŸãã¯HTMLã䜿ããããã Hello Worldãšããæååã衚瀺ããŸãã
説æã¯åŸãã«ãŸãšããŠããã®ã§ãã²ãšãŸãapp.pyãšrun.pyãæžããŠã¿ãŸãããã
from flask import Flask
#Flaskãªããžã§ã¯ãã®çæã
app = Flask(__name__)
#ã¢ã¯ã»ã¹ããURLãã/ãã ã£ãå Žåã"Hello World"ã®æååãè¿ã
@app.route("/")
def hello():
return "Hello World"
#ããŸããªã
if __name__ == "__main__":
app.run(debug=True)
from app.app import app
if __name__ == "__main__":
app.run()
ãããã®ã³ãŒããæžããããããšã¯WebãµãŒããç«ãŠãã ãã§ãã
ã³ãã³ããéãã以äžã®ããã«å®è¡ããŠã¿ãŠäžããã
python run.py
ããŸãè¡ããªãæ¹ã¯ãã£ã¬ã¯ããªã®ç§»åãåºæ¥ãŠããªãå¯èœæ§ããããŸãã
ãã£ã¬ã¯ããªã®ç§»åæ¹æ³ãåãããªãæ¹ã¯ãã¡ã
ã³ãã³ãããã³ãããªã©ã§äœãã®ãã¡ã€ã«ãå®è¡ããã«ã¯ããã®ãã¡ã€ã«ãä¿åãããŠãããã£ã¬ã¯ããªã«ç§»åããªããŠã¯ãããŸããã ä»åã§ãããšãrun.pyãå®è¡ããããã«ã¯å ã»ã©(any directory)ãšèšèŒããŠããç®æ(ç§ã®å Žåtestãšããååã®ãã©ã«ã)ã«ç§»åããå¿ èŠãããããã§ãã 移åã®ããã«ã¯è¡ãããå Žæã®äœæãå¿ èŠãªã®ã§ããšã¯ã¹ãããŒã©ãŒã§ç®çã®ãã£ã¬ã¯ããªã®çµ¶å¯Ÿãã¹ãååŸããŸãã 絶察ãã¹ãåãã£ãããã³ãã³ãã§ãcd 調ã¹ã絶察ãã¹ããå®è¡ããŠã¿ãŠäžããã ïŒcdã¯ãã§ã³ãžãã£ã¬ã¯ããªãè¡šããŠããŸããïŒ* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
ããã§è²°ããURLãhttp://127.0.0.1:5000/ ããã©ãŠã¶ã§éããŠã¿ãŠäžããã
Hello WorldãããªããåŸ
ã£ãŠããã¯ãã§ãã
ãã®äžé£ã®äœæ¥ããããšã³ãã³ãã®å
¥åãã§ããªããªãã®ã§ã䜿çšãåéãããå Žåã¯CTRL+CãæŒããŠãã ããã
ããã§ã¯app.pyã®äžèº«ã®èª¬æãããŠãããŸãã
####@app.route("/")ã«ã€ããŠ
å³å¯ãªããšã¯åãããªãã®ã§éã«ãããŸãã
@app.route("/")ããå®è¡ãããã®ã¯ãhttp://127.0.0.1:5000/ ã«ã¢ã¯ã»ã¹ãããšãã§ã
@app.route("/abc") ãå®è¡ãããã®ã¯ãhttp://127.0.0.1:5000/abc ã«ã¢ã¯ã»ã¹ãããšãã§ãã
####@app.route()å
ã®é¢æ°ã«ã€ããŠ
é¢æ°ã®å®çŸ©ã¯ããŠããŸãããäžèŠãããšå®è¡ã§ããŠããªãããã«èŠããŸãã
å§ãã¯ãäœæ
åãã®ã ïŒïŒãšçåã«æã£ãã®ã§ãããå®ã¯@app.route()ãæžãããé¢æ°ããã®ãŸãŸå®è¡ããä»çµã¿ã«ãªã£ãŠãããããã§ãã
ãããé¢æ°ã«ããå¿
èŠãããã®ãïŒãšãã話ã«ãªããŸãããæ£çŽããããããŸããã
ãã ãå¥ã®@ã§äœ¿ã£ãé¢æ°åãçšãããšãšã©ãŒãåºãã®ã§ãããã¯æ³šæããæ¹ãããããã§ãã
ãªããïŒã€ã®@ã®äžã§é¢æ°ãäºã€å®çŸ©ããŠã¿ããšãããïŒã€ç®ããå®è¡ãããŸããã§ããã
ä»åã¯return ã®æ»ãå€ãæååã ã£ãããããã æåã衚瀺ãããã ãã§ããã
次以éã§ã¯ã衚瀺çšã®HTMLãã¡ã€ã«ãçšããŠãããŸãã
##1-2. HTMLã䜿ã£ãŠç¶ºéºãªHello WorldïŒ
templateãã©ã«ãã«home.htmlãšããHTMLãã¡ã€ã«ãäœæããŸãã
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
ç¶ããŠãapp.pyã®æ¹ã以äžã®ããã«å€ããŸãã
render_templateãšãããhtmlã衚瀺ããé¢æ°ã䜿ããŸãããã
from flask import Flask,render_template
app = Flask(__name__)
@app.route("/")
def hello():
return render_template("home.html")
if __name__ == "__main__":
app.run(debug=True)
å ã»ã©ãšåæ§ã«å®è¡ãããšãè¯ãæãã® Hello World ãåºãŠããã¯ãã§ãã
##1-3. Hello Worldããã®Hello WorldïŒ
ç¶ããŠç»é¢éããå®è£
ããhome.htmlããå¥ã®ç»é¢ã«é£ã¹ãããã«ããŠã¿ãŸãããã
templatesã®äžã«ãsecond.htmlãšãããã¡ã€ã«ãè¿œå ããŸãã
ïŒäžèº«ã¯æ¬åœã«äœã§ãæ§ããŸãããhome.htmlãšåºå¥ã§ããã°å€§äžå€«ã§ããïŒ
ä»åã¯äžéšïŒïœïŒã¿ã°ã§å€ªåã«ããŠã¿ãŸããã
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Hello World <b>season2</b></h1>
</body>
</html>
ç¶ããŠãhome.htmlã«ãformæ å ±ãè¿œå ããŸãã
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Hello World</h1>
<form action="/second"><input type="submit" value="次ãž"></form>
</body>
</html>
formã®action=""ã®äžèº«ã¯ã次ã®URLãè¡šããŠããŸãã
value=""ã®äžèº«ã¯ãã¯ãªãã¯ããç®æã«è¡šç€ºããæåãè¡šããŠããŸãã
ããããããšã§ãç»é¢äžã®æå®ç®æãã¯ãªãã¯â次ã®URLã«é£ã¶ãšããæµããåºæ¥ãŸããã
ããã§ã¯ã次ã®URLãšå
ã»ã©äœæããhtmlãã¡ã€ã«ãçŽã¥ããŸãããã
app.pyã«ãæ°ããªURLãžã®å¯Ÿå¿ãè¿œå ããŸãã
from flask import Flask,render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/second")
def second():
return render_template("second.html")
if __name__ == "__main__":
app.run(debug=True)
ããã§ãç¡äºãHello World season2ãã衚瀺ã§ããããšãšæããŸãã
楜ããã§ãããããŸãè¡ãã°ã
#2.ããšããããç»å衚瀺ããŠã¿ã
ããã¯ç°¡åãªã®ã§ã身æ§ããªããŠã倧äžå€«ã§ãã
ä»åã¯éãé£ã¹ãŠããä»äººã®ç»åïŒKASUMI.pngïŒã衚瀺ããããšæããŸãã
staticãã©ã«ãå
ã«imagesãã©ã«ããäœæããããã«è¡šç€ºãããç»åãä¿åããŸãã
ïŒæ¡åŒµåã¯pngã§ãjpegã§ã倧äžå€«ã§ããïŒ
ä»åã¯home.htmlã以äžã®ããã«æ¹å€ããapp.pyã¯ãã®ãŸãŸã§ç»åã衚瀺ããŠãããŸãã
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<img src="\static\images\KASUMI.png" alt="éãé£ã¹ãŠããä»äºº" width="300" height="300">
</body>
</html>
alt=""ã®å
容ã¯ä»£æ¿ããã¹ããšåŒã°ãããã®ã§ãèŠèŠé害ã®ããæ¹çã®ããã®é³å£°èªã¿äžããªã©ã«äœ¿ããŸãããªããŠã倧äžå€«ã§ãããæžããŠãããŸãããã
widthãšheightã¯ç»åã®ãµã€ãºãæå®ãããã®ã§ããäœãæžããªããã°ããšããšã®ãµã€ãºã§è¡šç€ºãããŸãã瞊暪æ¯ã®æªã¿ãæ°ã«ãªãå Žåã¯çžŠãšæšªã®ã©ã¡ããã®ã¿ãæå®ããããšã§ãæ¯çãæãªãããšãªããµã€ãºèª¿æŽãåºæ¥ãŸãã
###ãã¹ãšã®éãâ
src=""ã®äžã«ã¯ããã¹ãæžããŸãã絶察ãã¹ã§ããã°æ¥œãªã®ã§ãããåŸè¿°ã®çç±ããçžå¯Ÿãã¹ã䜿ããŸãã
ãŸãåæãšããŠãçžå¯Ÿãã¹ã«ã¯ã¹ã¿ãŒãå°ç¹(èŠç¹)ããããŸãã
çµè«ããèšããšãçžå¯Ÿãã¹ã¯å®è¡ããå Žæãã¹ã¿ãŒãå°ç¹ãšããŠæžãããã§ããhome.htmlç®ç·ã§ã¯ãªãã®ã§ãã
ä»åã¯run.pyãåºæºãšããŠç»åã®æ ŒçŽãããŠãããã£ã¬ã¯ããªã®ãã¹ãæžãã®ã§ã
"\static\images\KASUMI.png"ãæ£ãããã¹ãšãªããŸããïŒæåã®\å¿ããã¡ïŒ
ããŸãè¡ããªã人ã¯ãã\ããã/ãã«å€ããŠã¿ãŠäžããã
ããã§ã¯ç»åã®å称ãã¢ã«ãã¡ãããè¡šèšã«ããŸããããæ¥æ¬èªã§ãäžå¿ãããŸãã
ããããã€ã§ããããéçšãããšã¯éããªãã®ã§ãå称ã¯ããšãªããã¢ã«ãã¡ãããè¡šèšã«ããŠãããŸãããã
ãã®èŸºãã§ããç»å衚瀺ã«çžå¯Ÿãã¹ãå¿
èŠãªããhtmlãã¡ã€ã«è¡šç€ºã®æã«ã¯ãªããããªãã£ãã®ïŒã
ãšããçåãæ±ããæ¹ãããã£ããããããããªãã®ã§èª¬æããŠãããŸãã
flaskã¯ãã¡ããã¡ãã«æèœãªã®ã§ãrender_template()ãšããç¥é¢æ°ã䜿ã£ããšãã¯èªåçã«ãtemplatesãã©ã«ãããhtmlãã¡ã€ã«ãæ¢ããŠããŠãããã®ã§ããè¶
絶䟿å©ãªåé¢ãtemplatesãªã©ã®ãã©ã«ãåãèŠå®éãã«èšå®ããªããšå³ãšã©ãŒãªã®ã§æ°ãä»ããŸãããã
render_template()ãçžå¯Ÿãã¹ãåæã«èŠç¹ã£ãŠããããšããããšã¯ã誰ãã©ããªç°å¢ã§å®è¡ããŠãåãçµæãè¿ãããšããããšã§ãã絶察ãã¹ã䜿ã£ãŠããŠã¯ããã®ãããªæ±çšæ§ãçã¿åºãããšã¯åºæ¥ãŸããã
ãã£ã¬ã¯ããªã®äœçœ®ãå€ããããäžåºŠæžããã³ãŒããä»ã®PCã§å®è¡ããããšã絶察ãã¹ã¯ããã«å€ãã£ãŠããŸããã®ã§ãããã®éœåºŠã³ãŒããæžãæããã®ã¯éåžžã«ã ããã®ã§ãä»åŸã匵ãåã£ãŠçžå¯Ÿãã¹ã«æãã§ãããŸãããã
ãªãããæ°ã¥ãã®æ¹ãããã£ããããšæããŸãããããã¯ç§ãšãã¹ãšã®é·ãéãã®åºç« ãå¹éãã«ãããŸããã
æ°ãåãçŽããŠrun.pyãå®è¡ããŸãããã
ãã®ãããªè¡šç€ºãåºãã°OKã§ãã
#3.ããã¡ã€ã«ã®ã¢ããããŒã
ããã¯å人çã«èŠããã ãã€ã³ãã§ããããã®èŠãã¿ãç¹°ãè¿ããªãããšãããæ¬èšäºã®ç®æšã§ãã
倧è¹ã«ä¹ã£ãã€ããã§èªãã§ãã£ãŠãã ããã
ãã®ç« ã§ã¯ãhome.htmlã§ãã¡ã€ã«ãã¢ããããŒãããŠãããããã¡ã€ã«ã®ä¿åå®äºã確èªãããšãããŸã§ãæ±ã£ãŠãããŸãã
å§ãã«ããã¡ã€ã«ã®ä¿åå
ãšããŠãstaticã®äžã«uploadedãšãããã©ã«ããäœã£ãŠãããŸãã
ããã®å称ã¯ãªãã§ãæ§ããŸããã
ãã©ã«ãã®æºåãã§ããããä¿åå®äºãç¥ãããããŒãžããäœã£ãŠãããŸãããã
templatesã®äžã«ãuploaded.htmlãšãããã¡ã€ã«ãäœæããŸãã
<!DOCTYPE html>
<html>
<body>
ä¿åãå®äºããŸããïŒåèªãèªåãžã®èªãèšèãæžããŠãããšããã§ããïŒ
</body>
</html>
ç¶ããŠãhome.htmlå
ã«ãã¡ã€ã«ã¢ããããŒãã®ãã©ãŒã ãšéä¿¡ãã¿ã³ãäœããŸãã
1è¡ç®ã®action=""ã®äžã«ã¯æ¬¡ã®URLãã
2è¡ç®ã®name=""ã®äžã«ã¯ä»®ã®ååãã
2è¡ç®ã®accept=""ã®äžã«ã¯åãå
¥ããæ¡åŒµåãæžããŸãã
ïŒãã以å€ã®èŠçŽ ãäœããŠããã®ãã¯æ£çŽããããã£ãŠããŸãããïŒ
<!DOCTYPE html>
<html>
<body>
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="uploaded_file" accept=".xls,.xlsx">
<input type="submit" value="éä¿¡">
</form>
</body>
</html>
次ã«ãapp.pyåŽãæ¹å€ããŠãããŸããããã¯å°ã倧å€ã§ãããåŸãã«è§£èª¬ãèŒããŠããã®ã§å®å¿ããŠãã ããã
from flask import Flask,render_template,request
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/upload", methods=["POST"])
def upload():
f = request.files["uploaded_file"]
filepath = 'app/static/' + f.filename
f.save(filepath)
return render_template("uploaded.html")
if __name__ == "__main__":
app.run(debug=True)
æ°ãã«ã€ã³ããŒãããrequestãšããã®ããã¡ããã¡ãåããŸãã
requestã䜿ã£ãŠãåã£ãŠãããã¡ã€ã«ã®æ±ããfã«äžä»»ãã
f.filenameã§ãã¡ã€ã«åã®ååŸ
f.save()ã§ãã¡ã€ã«ã®ä¿åããã£ãŠããããšããæãã§ããã
ãããã€ã³ã¹ã¿ã³ã¹åã«å€ããã®ãã©ããã¯ãèªä¿¡ããªãã®ã§èšåããªãããšã«ããŸãã
###ãã¹ãšã®éãâ¡
ãŸãæ¥ãŠããŸããŸãããããã§ã¯ç§ãèŠãããfilepathã«ã€ããŠè¿°ã¹ãŠãããŸãã
flaskã®requestãsaveã«ã€ããŠèª¿ã¹ããšãsave()ã«æž¡ãã®ã¯ãä¿åå
ã®ãã¹ããšæžãããŠããããšãå€ãã§ãã
ããããªããã"app/static/"ãã ãã§ã¯ã©ãé 匵ã£ãŠããšã©ãŒã«ãªã£ãŠããŸããŸãã
ãã®åå ã¯ãæ±ããããŠãããã¹ããä¿åå
ã®ãã©ã«ãã®ãã¹ãã§ã¯ãªãããä¿åãããåŸã®ããã¡ã€ã«èªèº«ã®çžå¯Ÿãã¹ãã§ããããšã§ããã
ãã®ããfilepathã«ã¯ãf.filennameã§ååŸãããã¡ã€ã«åïŒæ¡åŒµåä»ãïŒãŸã§å
¥ããªããŠã¯ãªããªãã®ã§ãã
/ã®æç¡ãã¹ãã«ãã¹ãªã©ã«ãåå泚æããããã§ã䜿ããã ããã
ãããŸã§æºåãåºæ¥ãããrun.pyã®å®è¡ã§ãã
URLãéããããèšãããããŸãŸé©åœãªãã¡ã€ã«ãã¢ããããŒãããŸãããã
ããŸãè¡ãã°ç¥çŠã®ã¡ãã»ãŒãžãšãšãã«ãuploadedãžãã¡ã€ã«ãä¿åãããã¯ãã§ãã
åºåããããã®ã§ååã¯ãããŸã§ã«ããŸããåŸåã¯ãã¡ãâHTMLãHTLMãããç¥ããªãã£ãåå¿è ã«ãããWebã¢ããªãšã®æ Œéã®çè·¡(åŸå)