æ¬èšäºã®æ§æã¯ä»¥äžã®ããã«ãªã£ãŠããŸãã
0.ãéçºåã®äžæºåãç°å¢æ§ç¯ããä»®æ³ç°å¢ã¯ãªããŠããªããšããªãã
1.ãWebã¢ããªã®åºæ¬çãªæ§é ããURLã«é ŒããŸããã
2.ããšããããç»å衚瀺ããŠã¿ãããããããŠããã¹ã£ãŠãšãã§ããªãé¢åãããïŒ
3.ããã¡ã€ã«ã®ã¢ããããŒãããããããããŠããã¹ã£ãŠãšãã§ããªãé¢åãããïŒ
4.ã@ã®å€åŽã«å€æ°ãé£ããŠãããsessionã¯ç¥ã ãäžèœã§ã¯ãªãã£ãããã
5.ãPythonã§ã®æœéžåŠçãããããäŸå€ãã¿ãŒã³ãæå€ãšå€ã
6.ãé¢æ°ãå€ããé£ããŠããããããããããŠããã¹ã£ãŠãšãã§ããªãé¢åãããïŒ
7.ãçµæã®åºåãããããããããcsvãexcelããPandasã®ãããã§æå€ãšç°¡å
8.ãèŠãç®ãæŽãããããããããããã§ç»å ŽHTLM
åŸåã§ã¯ã4çªããåãçµãã§ãããŸããååã¯ãã¡ãâHTMLãHTLMãããç¥ããªãã£ãåå¿è ã«ãããWebã¢ããªãšã®æ Œéã®çè·¡(åå)
#4.ã@ã®å€åŽã«å€æ°ãé£ããŠãã
ã¢ããããŒãã§ãããã¡ã€ã«ããå
¥åãããæåãªã©ã®æ
å ±ã¯ãä¿åããã ãã§ãªãå©çšããããã®ã§ãã
äŸãã°å
ã»ã©ã®filepathãªã©ã¯ããã®åŸã¬ã³ã¬ã³äœ¿ã£ãŠãããããã®ã®äžã€ã§ãããå®ã¯ãã®ãŸãŸã§ã¯äœ¿ãåæãéåžžã«æªãã®ã§ãã
æ
é·ã§ãã@app.route("/uploaded")ã®äžã§ããã°åœç¶ãfilepathã¯ç«æŽŸãªçããå€æ°ã§ããããããäžåºŠå¥ã®URLã«é£ã³å¥ã®@åå²ã«è¡ããšãfilepathã¯å€æ°ãšããŠèªããŠããããŸãããéåžžã«æ²ããã§ããã
ããã§äœ¿ãããã®ãflaskã®sessionãšãããã®ã§ãã
ããã¯ç°¡åãªããŒã¿ããŒã¹ã®ãããªãã®ã§ãsessionã«ç»é²ãããŠããå€æ°ãªãå¥ã®åå²ã«ããŠã䜿ããŸãã
ããã䜿ãæ¹ã¯æ»
è¶æ»
è¶ç°¡åãªã®ã§ãfilepathãäŸã«æ©éèŠãŠãããŸããã
åèã«ããèšäºâãFlaskãSessionã«ã€ããŠ
####sessionãžã®ç»é²
å§ãã«ãã·ãŒã¯ã¬ããããŒãšããã®ãèšå®ããŸããäœã§ããããããã§ãã
app.secret_key = 'yeahyeah'
äžåºŠã·ãŒã¯ã¬ããããŒãèšå®åºæ¥ãããåã ã®ç»é²ã¯ãã®ããã«è¡ããŸãã
session["filepath"] = filepath
ããã§ãæ
é·ã®@app.route()ã®å€ã«åºãããããã«ãªããŸãã
####sessionããã®èªã¿èŸŒã¿
filepath = session["filepath"]
#ãŸãã¯
filepath = session.get("filepath")
æ é·ã®@app.route()ã®å€ã§ãããã®ããã«åŒã³åºãã°æ®éã®å€æ°ãšããŠãã®åå°ã«éŠŽæããã®ã§ãã
ä»åã¯sessionã䜿ããã¢ããããŒãããããã¡ã€ã«ãå¥ã®åå²ã§pandasã®DataFrameã«å å·¥ããããšæããŸãã
ïŒãããããexcelãã¡ã€ã«ãæ³å®ããŠé²ããŠãããŸããïŒ
DataFrameã«å å·¥ã§ãã蚌æã®ãããä¿åå®äºã®æ¬¡ã®ç»é¢ã§ãDataFrameã®è¡æ°ã衚瀺ããŠã¿ãŸãããã
uploaded.htmlã®æ¹å€ãšãgyousuu.htmlã®æ°èŠäœæãè¡ããŸãã
<!DOCTYPE html>
<html>
<body>
ä¿åãå®äºããŸããïŒåŠçãè¡ããŸãã
<form action="/gyousuu"><input type="submit" value="è¡æ°ã¯ãã¡ã"></form>
</body>
</html>
ããŒãžéãã®ããã®ãã¿ã³ãè¿œå ããŸããã
ç¶ããŠè¡æ°è¡šç€ºçšã®htmlãã¡ã€ã«ã§ããããšãããã以äžã®ããã«ããŠã¿ãŠäžããã
<!DOCTYPE html>
<html>
<body>
ã¢ããããŒããããExcelãã¡ã€ã«ã¯{% print(session.get("gyousuu")) %}è¡ã§ããïŒ
</body>
</html>
é©ãã»ã©ãããããä»çµã¿ãªã®ã§ãããå®ã¯{%%}ã§å²ãããšã«ãã£ãŠãPythonã䜿ããŸãã
ããã§ã¯printæã®()ã®äžã«ãsessionããåç
§ããŠããgyousuuãšããå€æ°ãå
¥ããããé©åãªè¡æ°ã衚瀺ãããä»çµã¿ã§ããprintæã¯ç°¡æè¡šçŸãçšæãããŠããããã{{å€æ°å}}ã ãã§ã衚瀺ã§ããŸãã
sessionã«ä¿åãããŠããå€æ°ãªãã°ãhtmlãã¡ã€ã«ããã§ãPythonãã¡ã€ã«ããã§ãåç
§ã§ããããã§ãã
ãšããããã§ããã£ãŠã¿ãŸãããïŒ
from flask import Flask,render_template,request,session
import pandas as pd
app = Flask(__name__)
app.secret_key = 'yeahyeah'
@app.route("/")
def home():
return render_template("home.html")
@app.route("/upload", methods=["POST"])
def upload():
f = request.files["uploaded_file"]
filepath = 'app/static/uploaded/' + f.filename
f.save(filepath)
#sessionã«filepathãç»é²
session["filepath"] = filepath
return render_template("uploaded.html")
@app.route("/gyousuu")
def gyousuu():
#sessionããfilepathãåç
§
filepath = session.get("filepath")
#PandasãçšããŠãšã¯ã»ã«ãã¡ã€ã«ããæ±ããããdataframeã«å å·¥ã
df = pd.read_excel(filepath)
gyousuu = len(df)
#sessionã«gyousuuãç»é²
session["gyousuu"] = gyousuu
return render_template("gyousuu.html")
if __name__ == "__main__":
app.run(debug=True)
ãã¡ãããäœã§ãããã§ãsessionã«å
¥ããããããã§ã¯ãããŸããã
äŸãã°dataframeãªã©ã¯sessionã«å
¥ããªãã®ã§ãå¥ã®æ¹æ³ã§é£ããŠè¡ããªããŠã¯ãªããªãããšã«ãªããŸãã
ããã§ã¯ãã®äžã€ãšããŠããçŽéãã玹ä»ããŸãã
ã次ã«è¡šç€ºããHTMLãã¡ã€ã«ãã«å€æ°ãé£ããŠããå Žåãã䜿ããªãã®ã§ãããrender_template()ã®åŒæ°ãšããŠçŽæ¥æå®ããŠããŸãã°ãhtmlãã¡ã€ã«å
ã§ããã®å€æ°ã䜿ããŸãã
render_template("gyousuu.html", gyousuu=gyousuu)
ãããŸã§ã§ããã°ãåºæ¬çãªã¢ããªãäœãããããªæ°åã«ãªã£ãŠãããšæããŸãã
#5.ãPythonã§ã®æœéžåŠç
ã ãã¶é·ãéã®ãã§ããããå®ã¯ããã¯æœéžã¢ããªã®äœæãªã®ã§ããã®ã¡ã€ã³éšåãäœããªããŠã¯ãªããŸãããåŸã
main.pyã«çµã¿èŸŒãããã«ãé¢æ°ã®åœ¢ã§æœéžåŠçãããŠãããŸãã
ïŒå¿
èŠã®ãªã人ã¯èªã¿é£ã°ããŠãã ãããïŒ
æ¬ã¢ããªã¯15ãã€äœã£ãŠããŸã£ãTã·ã£ããšããŒãããã°ãåžæè
ã«æãæœéžã¢ããªãªã®ã§ãåžæè
ã®ååãšåžæã®ååã®Excel(ã®ãã¹)ãåŒæ°ã«ããåœéžè
ã®ããŒã¿ãã¬ãŒã ãè¿ãé¢æ°ãäœããŸãã
import pandas as pd
def choose(pas):
#ããŒã¿ã®èªã¿èŸŒã¿
df = pd.read_excel(pas)
df.dropna(how='any')
#åžæããšã«ã°ã«ãŒãåã
bag = df[df["åžæ"]=="ããŒãããã°"]
tst = df[df["åžæ"]=="Tã·ã£ã"]
kibou_bag = len(bag)
kibou_tst = len(tst)
#äžæ¬æãªæ¹ã§åœéžãã人æ°
huhonni_bag = 0
huhonni_tst = 0
if kibou_bag > 15 and kibou_tst < 15:
hæ
å ±onni_tst = min((15-kibou_tst), (kibou_bag-15))#äœã£ãTã·ã£ããšãããã°è²°ããªãã£ã人æ°ãæ¯èŒ
elif kibou_tst > 15 and kibou_bag < 15:
huhonni_bag = min((15-kibou_bag), (kibou_tst-15))#äœã£ãããã°ãšãTã·ã£ãè²°ããªãã£ã人æ°ãæ¯èŒ
#æœéž
if huhonni_bag == 0 and huhonni_tst == 0:
bag_get = bag.sample(min([15,kibou_bag]))
tst_get = tst.sample(min([15,kibou_tst]))
elif huhonni_bag >0 :
tst_get = tst.sample(15+huhonni_bag)
bag_get = pd.concat([bag, tst_get.tail(huhonni_bag)])
tst_get = tst_get.head(15)
elif huhonni_tst >0 :
bag_get = bag.sample(15+huhonni_tst)
tst_get = pd.concat([tst, bag_get.tail(huhonni_tst)])
bag_get = bag_get.head(15)
bag_get.reset_index(inplace=True, drop=True)
tst_get.reset_index(inplace=True, drop=True)
kekka = pd.concat([bag_get["åå"], tst_get["åå"]], axis=1)
kekka.columns=["ããŒãããã°","Tã·ã£ã"]
return kekka
èšäºã®é·ãã«ããããããŠããã®ã§ãããã®è§£èª¬ã¯çãããŠãã ããã
ãšããããæžãäžããããšã倧åã§ãã
#6.ãé¢æ°ãå€ããé£ããŠãã
ããŠã話ãã¢ããªéçºã£ãœããšããã«æ»ããŸãã
çŸç¶äœ¿ã£ãŠããPythonã®ãã¡ã€ã«ã¯æ®ã©app.pyã ãã§ãããäžèº«ãè€éã«ãªããšããã§ã¯ãã£ãŠãããªããªããŸããå®åéšåãä»ã®.pyãã¡ã€ã«ã«ä»»ããŠããªãã¹ãapp.pyãç°¡ç¥åããããšãã欲æ±ãåºãŠãããã®ãšããæã
ã¯ãŸãããå çžã®ãã¹ãšå¯Ÿå³ããããšã«ãªãã®ã§ãã
é¢åã ïŒãšããæ¹ã¯ãapp.pyå
ã§é¢æ°ã®å®çŸ©ãè¡ã£ãŠããŸã£ãŠãã ããã
ç°¡æçãæé
- appãã©ã«ãå ã«ãpythonãã¡ã€ã«ãæ°èŠäœæããã
- ãã®ãã¡ã€ã«å ã§ãå®åéšåã®é¢æ°ãäœãã
- çžå¯Ÿãã¹ã§ãã®ãã¡ã€ã«ãæå®ããããããç®çã®é¢æ°ãimportããŠããã
- é©å®äœ¿ãã
###1.appãã©ã«ãå
ã«ãpythonãã¡ã€ã«ãæ°èŠäœæããã
ãä»åã¯appãã©ã«ãå
ã«ãChusen.pyãšãããã¡ã€ã«ãäœããŸããã
###2.ãã®ãã¡ã€ã«å
ã§ãå®åéšåã®é¢æ°ãäœãã
ãããã¯ã²ãšã€åã®ç« ã§æ¢ã«è¡ã£ãŠããŸãã
ä»åã¯ãåžæ調æ»ã®Excelãã¡ã€ã«ã®çžå¯Ÿãã¹ãpasãšããåŒæ°ã«ããŠããŸãããåŒæ°ã¯ãã£ãŠããªããŠãå
šç¶å€§äžå€«ã§ãã
###3.çžå¯Ÿãã¹ã§ãã®ãã¡ã€ã«ãæå®ããããããç®çã®é¢æ°ãimportããŠããã
ãçµè«ããèšããšãapp.pyã§æ¬¡ã®ãããªã€ã³ããŒããè¡ãã°OKã§ãã
from app.chusen import choose
ãã®"app.chusen"ãšããã®ã¯ãå®è¡ãã¡ã€ã«ã§ããrun.pyç®ç·ã®çžå¯Ÿãã¹ã§ãã
appãã¡ã€ã«ã®äžã®chusenãšãããã¡ã€ã«ãããchuooseãšããé¢æ°ãæã£ãŠããŠãããšããæ瀺ãªããã§ãã
###4.é©å®äœ¿ãã
ããã§ã¯äœ¿ã£ãŠãããŸããããå
ã«app.pyã®æ¹ãæ¹å€ããŸãã
/chusenãšããURLã«ã以äžã®åŠçã察å¿ãããŸãã
ãchusen()ã®æ»ãå€ãkekkaã«æ ŒçŽããæœéžçµæä¿åã®ããExcelã§åºåããŠããŸãã
ãæ®å¿µãªããdataframeã¯sessionã«ä¿åã§ããªãã®ã§ãçŽéæ¹åŒã§ãããŸãã
@app.route("/chusen")
def chusen():
filepath = session.get("filepath")
kekka = choose(filepath)
session["kekka"] = kekka
kekka.to_excel("kekka.xlsx")
return render_template("kekka.html", kekka=kekka)
次ã«ãuploaded.htmlãæ¹å€ããŠãããŸãã
<!DOCTYPE html>
<html>
<body>
<title>ã¢ããããŒãæå</title>
<form action="/chusen"><input type="submit" value="æœéžéå§"></form>
</body>
</html>
ããã§ãã¢ããããŒãå®äºåŸãæœéžãè¡ããšãããŸã§åºæ¥ãŸããã
ããšã¯kekka.htmläžã§çµæã衚瀺ã§ããã°å®æã§ãã
ã¡ãããšããããæ¹ã¯ãsys.path.append()ã§æ€çŽ¢
äžèšã®æ¹æ³ã¯ãçžå¯Ÿãã¹ãåãã£ãŠããå Žåã®æ¹æ³ã§ãã
ãããå®éã®éçºç°å¢ã§ã¯ããã®éšåã ããå®è¡ãããïŒçã®çç±ã§å®è¡ãã¡ã€ã«èªäœãå€ãã£ãŠããŸãããšãããããšæããŸãã
ãããªããšãæ¯åçžå¯Ÿãã¹ãæžãæ¿ããå¿
èŠãåºãŠããŠããŸããããã ããã¹ãå¢ããŠããŸãããã§ãã
äœèšãªæéãå¢ãããªãããã«ã¯ãçžå¯Ÿãã¹ãèªåã§ååŸããŠãããsys.path.appendãšãããè¶
絶䟿å©ãªãããªã®ã§ãæ°ã«ãªãæ¹ã¯æ¯é調ã¹ãŠã¿ãŠäžããïŒ
#7.ãçµæã®åºå
çµæã®è¡šç€ºã¯ã以äžã®ãããªæãã§ãã£ãŠã¿ãŠäžããïŒ
è€æ°è¡šç€ºãããã®ã§ãforæã䜿ã£ãŠããŸãã
æ®éã®Pythonãšç°ãªãç¹ã¯2ã€ã§ãã
ãŸãã{% print(name) %}ã®ä»£ããã«{{name}}ãšããç°¡æè¡šçŸã䜿ã£ãŠãããšããç¹ã
ãããŠãforæã®æåŸã«endforãæžããŠãããšããç¹ã§ãã
<!DOCTYPE html>
<html>
<body>
Tã·ã£ãåœéžè
çºè¡š
<br>
{% for name in kekka["ããŒãããã°"] %}
{{name}}<br>
{% endfor %}
</body>
</html>
èŠæ ãã«ãã ãããªããã°ãæå€ãšç°¡åã«è¡šç€ºã§ããŸãã
#8.ãèŠãç®ãæŽãã
ããããã¯HTMLã®è©±ã«ãªã£ãŠããŸãã
æäœéå¿
èŠãªæ¹è¡ãäžå€®å¯ãã®ãããã¯è§£èª¬ãããŸãã
ãããŠãã®åŸãå
šãç解ããã«äœ¿ã£ãŠããç»é¢åå²ãèæ¯ã®è²å€ãã®éããæåããäŸãèŒããŠãããŸãã
####æ¹è¡
䜿ãã¿ã°ã¯ïŒbrïŒã§ããæ®ã©ã®ã¿ã°ã2ã€ã®ã¿ã°ã§è©²åœç®æãæãã®ã«å¯Ÿããæ¹è¡ã¿ã°ã¯æ¹è¡ãå
¥ããŠæ¬²ãããšããã§1ã€ã ã䜿ããŸããéã«èšããšãhtmläžã§ã®æ¹è¡ã«ã¯æ®ã©æå³ããªããšããããšã§ãã
èŠãããããããã«ãã¬ã³ã¬ã³æ¹è¡ããŠããŸããããè¡ã®æåã®ã¹ããŒã¹ãªã©ã調æŽããŠãèŠãããHTMLãå¿ãããããšæããŸãã
<!DOCTYPE html>
<html>
<body>åå¿è
ã®çãã<br>ããã«ã¡ã</body>
</html>
<!DOCTYPE html>
<html>
<title>ãã¡ã€ã«ãã¢ããããŒãããŠãïŒ</title>
<body>
<br>
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="excel" accept=".xls,.xlsx">
<br>
<input type="submit" value="éä¿¡">
</form>
â»ãã¡ã€ã«åœ¢åŒã¯excelã§ãé¡ãããŸãã
</body>
</html>
####äžå€®å¯ããšãããã¯åå²
æå衚瀺ã¯åæèšå®ã ãšå·Šç«¯ã«å¯ã£ãŠããŸããŸãã
ãã®ãããäžå€®ã«å¯ãããå Žåã¯è©²åœç®æãïŒcenterïŒãšïŒ/centerïŒã§å²ãå¿
èŠããããŸãã
ãŸãããããããããŸã§ã¯ã²ãšãŸãšãã«ãããããšããå Žåã¯è©²åœç®æãïŒdivïŒãšïŒ/
divïŒã§å²ã£ãŠãããã¯ã«ããŸããæ£çŽã«èšããšãããã¯åãã®ååšæ矩ã¯ãŸã åãããªãã®ã§ããã倧äºãªæ°ãããã®ã§èŒããŠãããŸãã
<!DOCTYPE html>
<html>
<title>ãã¡ã€ã«ãã¢ããããŒãããŠãïŒ</title>
<body>
<br>
<center>
<form action="/upload" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name="excel" accept=".xls,.xlsx">
</div>
<br>
<div>
<input type="submit" value="éä¿¡">
</div>
</form>
â»ãã¡ã€ã«åœ¢åŒã¯excelã§ãé¡ãããŸãã
</center>
</body>
</html>
ãããå®è¡ãããšããã®ãããªç»é¢ã«ãªãã¯ãã§ãã
####ç»é¢åå²ãšèæ¯ã®è²å€ã
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>çµæçºè¡š</title>
<style>
#child1 {
background-color: rgb(243, 181, 162);
}
#child2 {
background-color: rgb(148, 195, 233);
}
@media (min-width: 600px) {
#parent {
display: flex;
}
#child1 {
flex-grow: 1;
}
#child2 {
flex-grow: 1;
}
}
</style>
</head>
<body>
ã<div id="parent">
<div id="child1">
<center>
<table>
<p><font size="5">ããŒãããã°åœéžè
</font></p>
<br>
{% for name in kekka["ããŒãããã°"] %}
{% print(name) %}<br>
{% endfor %}
</table>
<img src="/static/images/bag.jpeg" alt="ããŒãããã°" width="200" height="200">
</div>
</center>
<div id="child2">
<center>
<table>
<p><font size="5">Tã·ã£ãåœéžè
</font></p>
<br>
{% for name in kekka["Tã·ã£ã"] %}
{{name}}<br>
{% endfor %}
</table>
<img src="\static\images\tst.png" alt="Tã·ã£ã" width="200" height="200">
</div>
</center>
ã</div>
ã<div id="parent">
<center>
<form action="/fin">
<input type="submit" value="次ãž">
</form>
</center>
</div>
</body>
</html>
ããäœãäœãè¡šããŠããã®ãåãããŸããããchildãšparentã«åãããŠããã®ã¯åãããŸãã
å®è¡ãããšãã®ãããªç»é¢ã«ãªããŸãã
#ãŸãšããšã瀌ãšææ³ã¡ã¢
ãããŸã§èªãã§äžãããæ¬åœã«ããããšãããããŸããã
ååŸç·šã®åèšïŒç« ã§ãæäœéã¢ããªãäœãã®ã«å¿
èŠãªç¥èã«ã¯è§ŠããããããšæããŸãã
ã¢ããªå¶äœãšããé«ãå£æ ¹ã®ãåãããšãã¡ããçµã¶èšäºã«ãªããŠããã幞ãã§ãã
æ¬èšäºã®å·çäžã倧åŠã®ææ¥ã§PythonãšCèšèªã亀äºã«åŠã¶ãšããçµéšãããŸããã
æ¯èŒã®æå³ã®åŒ·ãè¬çŸ©ã ã£ãããšããããPythonã®å¿åºŠã¹ãã«ã«æ°ä»ããããèªåããšãã§ããªãçãããããŠãããšããèªèŠãèœçããŸãããdataframeãsessionã«ä¿åã§ããªãïŒãšåããŠããèªåã«æããŠããããã§ããå宣èšããªãintãdoubleãstrãåãå
¥ããŠãããsessionãããã«æé£ãååšãã
åœåã¯ããèªåã®å³ãã£ãèŠãã¿ãä»ã®èª°ããç¹°ãè¿ããªãããã«ããšå§ããèšäºã§ããããæžããŠããå ã«æ°ããªçºèŠãå€æ°ãããå¥ã®çš®é¡ã®èŠãã¿ãšæé·ãå³ãããŸãããå人ã«ã¯ãµã¯ã©ããã¡ããªã¢ãšåŒã°ããŠããèšäºã§ãããæããªããããªããšãæžãäžããããšãåºæ¥ãŠããã£ãã§ãã