LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

Flask step2

Last updated at Posted at 2023-10-05

python の復習

リスト

app.py
def index():
    mylist=[0,1,1,3,3,3]
    print(mylist)
    return (str(mylist))

return (str(mylist[2]))に変更して実行してみよう。

app.py
def index():
    mylist=[[0,1],[1,1],[2,1]]
    print(mylist)
    return (str(mylist))

return (str(mylist[0][1]))に変更して実行してみよう。

文字列の置換

app.py
def index():
    a="ああああああああ"
    a=a.replace("","")
    return a

csvの内容をリストに入れる

app.py
from flask import Flask, render_template, request, session, redirect,flash, url_for
from csv import reader

app = Flask(__name__)

# 編集ゾーンーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
##ここでルートのURLが指定された時に実行される
@app.route('/')
def index():
    #index.htmlがレンダリングされる
    with open('./mmcsv.csv', 'r') as csv_file:
        csv_reader = reader(csv_file)
        list_mm = list(csv_reader)
    return render_template("index.html",mmlist=list_mm)

# ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
if __name__ == '__main__':
    app.run()
index.html
    <h1>テスト</h1>
    <div>
        {{ mmlist }}
    </div>
index.html
    <h1>テスト</h1>
    <div>
        {% for data in mmlist %}
            {{ data[1] }},{{ data[3] }} <br>
        {% endfor %}
        
    </div>
app.py
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(" ","")

    print(list_mm)
    return render_template("index.html",mmlist=list_mm)
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up