Flask
コード
app.py
- from flask import Flask, render_template, request, session, redirect,flash, url_for
- from csv import reader,writer
- app = Flask(__name__)
- # 編集ゾーンーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
- ##ここでルートのURLが指定された時に実行される
- def get_id():
- with open('./mmcsv.csv', 'r') as csv_file:
- csv_reader = reader(csv_file)
- list_mm = list(csv_reader)
- max_value=0
- for data in list_mm:
- if data[0]!='ID':
- if int(data[0])>max_value:
- max_value=int(data[0])
- new_id=max_value+1
- return new_id
- @app.route('/')
- def index():
- #index.htmlがレンダリングされる
- with open('./mmcsv.csv', 'r') as csv_file:
- csv_reader = reader(csv_file)
- list_mm = list(csv_reader)
- for data in list_mm:
- data[3]="https://www.google.com/maps?q=" + data[3].replace("(","").replace(")","").replace(" ","").replace(".",".")
-
- return render_template("index.html",mmlist=list_mm)
- @app.route('/input')
- def move_to_input():
- return render_template("input.html")
- @app.route('/reg',methods=["POST"])
- def registration():
- place = request.form["place"]
- content = request.form["content"]
- longitude_latitude = '(' + request.form["longitude_latitude"] + ')'
- hashtag = request.form["hashtag"]
- tips = request.form["tips"]
- id = get_id()
- data = [[id,place,content,longitude_latitude,hashtag,tips]]
- # CSVファイルに書き込む
- with open('./mmcsv.csv', 'a', newline='', encoding='utf-8') as file:
- csv_writer = writer(file)
- csv_writer.writerows(data)
- return redirect(url_for('index'))
- with open('./mmcsv.csv', 'a', newline='', encoding='utf-8') as file:
- csv_writer = writer(file)
- csv_writer.writerows(data)
-
- return redirect(url_for('index'))
-
- # ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
- if __name__ == '__main__':
- app.run()
index.html
- <!DOCTYPE html>
- <html lang="jp">
- <head>
- <title>flaskテスト</title>
- <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, user-scalable=yes">
- <meta charset="UTF-8">
- <link rel="stylesheet" href="../static/css/style.css">
- </head>
- <body>
- <h1>テスト</h1>
- <a href="{{ url_for('move_to_input')}}">登録ページ</a>
- <div>
- {% for data in mmlist %}
- {% if data[1] == "場所" %}
- {{ data[1] }}<br>
- {% else %}
- <a href ={{ data[3] }} target="_blank">{{ data[1] }} </a>:{{ data[2] }}<br>
- {% endif %}
- {% endfor %}<br>
-
- </div>
- </body>
- </html>
input.html
- <!DOCTYPE html>
- <html lang="jp">
- <head>
- <title>登録ページ</title>
- <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, user-scalable=yes">
- <meta charset="UTF-8">
- <link rel="stylesheet" href="../static/css/style.css">
- </head>
- <body>
- <h1>登録</h1>
- <form method="POST" action="{{ url_for('registration')}}" enctype="multipart/form-data">
- 場所<br>
- <input type="text" name="place" size="35"><br>
- 内容<br>
- <input type="text" name="content" size="150"><br>
- 緯度経度<br>
- <input type="text" name="longitude_latitude" size="100"><br>
- ハッシュタグ<br>
- <input type="text" name="hashtag" size="100"><br>
- 豆知識<br>
- <input type="text" name="tips" size="150"><br>
- <br><br>
- <input type="submit" value="登録">
- </form>
- </body>
- </html>