Flask

コード

app.py

  1. from flask import Flask, render_template, request, session, redirect,flash, url_for
  2. from csv import reader,writer
  3. app = Flask(__name__)
  4. # 編集ゾーンーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
  5. ##ここでルートのURLが指定された時に実行される
  6. def get_id():
  7.     with open('./mmcsv.csv', 'r') as csv_file:
  8.         csv_reader = reader(csv_file)
  9.         list_mm = list(csv_reader)
  10.         max_value=0
  11.         for data in list_mm:
  12.             if data[0]!='ID':
  13.                 if int(data[0])>max_value:
  14.                     max_value=int(data[0])
  15.     new_id=max_value+1
  16.     return new_id
  17. @app.route('/')
  18. def index():
  19.     #index.htmlがレンダリングされる
  20.     with open('./mmcsv.csv', 'r') as csv_file:
  21.         csv_reader = reader(csv_file)
  22.         list_mm = list(csv_reader)
  23.         for data in list_mm:
  24.             data[3]="https://www.google.com/maps?q=" + data[3].replace("(","").replace(")","").replace(" ","").replace(".",".")
  25.             
  26.     return render_template("index.html",mmlist=list_mm)
  27. @app.route('/input')
  28. def move_to_input():
  29.     return render_template("input.html")
  30. @app.route('/reg',methods=["POST"])
  31. def registration():
  32.     place = request.form["place"]
  33.     content = request.form["content"]
  34.     longitude_latitude = '(' + request.form["longitude_latitude"] + ')'
  35.     hashtag = request.form["hashtag"]
  36.     tips = request.form["tips"]
  37.     id = get_id()
  38.     data = [[id,place,content,longitude_latitude,hashtag,tips]]
  39. # CSVファイルに書き込む
  40.     with open('./mmcsv.csv', 'a', newline='', encoding='utf-8') as file:
  41.         csv_writer = writer(file)
  42.         csv_writer.writerows(data)
  43.     return redirect(url_for('index'))
  44.     with open('./mmcsv.csv', 'a', newline='', encoding='utf-8') as file:
  45.         csv_writer = writer(file)
  46.         csv_writer.writerows(data)
  47.     
  48.     return redirect(url_for('index'))
  49.     
  50. # ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
  51. if __name__ == '__main__':
  52.     app.run()

index.html

  1. <!DOCTYPE html>
  2. <html lang="jp">
  3. <head>
  4.     <title>flaskテスト</title>
  5.     <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, user-scalable=yes">
  6.     <meta charset="UTF-8">
  7.     <link rel="stylesheet" href="../static/css/style.css">
  8. </head>
  9. <body>
  10.     <h1>テスト</h1>
  11.     <a href="{{ url_for('move_to_input')}}">登録ページ</a>
  12.     <div>
  13.         {% for data in mmlist %}
  14.             {% if data[1] == "場所" %}
  15.             {{ data[1] }}<br>
  16.             {% else %}
  17.             <a href ={{ data[3] }} target="_blank">{{ data[1] }} </a>:{{ data[2] }}<br>
  18.             {% endif %}
  19.         {% endfor %}<br>
  20.         
  21.     </div>
  22. </body>
  23. </html>

input.html

  1. <!DOCTYPE html>
  2. <html lang="jp">
  3. <head>
  4.     <title>登録ページ</title>
  5.     <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, user-scalable=yes">
  6.     <meta charset="UTF-8">
  7.     <link rel="stylesheet" href="../static/css/style.css">
  8. </head>
  9. <body>
  10.     <h1>登録</h1>
  11.     <form method="POST" action="{{ url_for('registration')}}" enctype="multipart/form-data">
  12.         場所<br>
  13.         <input type="text" name="place" size="35"><br>
  14.         内容<br>
  15.         <input type="text" name="content" size="150"><br>
  16.         緯度経度<br>
  17.         <input type="text" name="longitude_latitude" size="100"><br>
  18.         ハッシュタグ<br>
  19.         <input type="text" name="hashtag" size="100"><br>
  20.         豆知識<br>
  21.         <input type="text" name="tips" size="150"><br>
  22.         <br><br>
  23.         <input type="submit" value="登録">
  24.     </form>
  25. </body>
  26. </html>