ファイル名:「main.py」更新日:2024/12/8

2024年12月8日日曜日

source code 公開

t f B! P L

       



 
#******************************************************************************#
# ファイル名:main.py
# バージョン:Ver1.0
# 機能:
# 作成日:2024/11/25
# 更新日:2024/12/8 W005

#*******************************************************************************#
import os
import json

file_path = 'KDjson.json'
# ★render_template をインポート
from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def hello_show():
    return 'Hello World!'

@app.route('/index')
def index():
  # ★(1)追記
    insert_dict = {
        'insert_1': 'insert_1部分です。',
        'insert_2': 'insert_2部分です。',
    }
  # ★(2)修正
    return render_template('index.html', title='Webアプリテスト', insert_dict=insert_dict)

@app.route('/kakeibo')
def kd():
    # 既存ファイルを確認
    if not os.path.exists(file_path):
        # 無ければ作成
        with open( file_path, mode='w', encoding="UTF-8" ) as open_json:
            initial_list = [{
                            "month"   :  0    , #"月"
                            "day"     :  0    , #"日"
                            "content" : "内訳",
                            "amount"  :  0      #"金額"
            } ]
            json.dump( initial_list, open_json, indent=4, ensure_ascii=False)

    # JSONファイルを読み込んで、辞書型リストを作成
    with open( file_path, mode='r', encoding="UTF-8") as open_json:
        json_load_list = json.load(open_json)

    # 辞書型リストからデータを受け取り
    rows = []
    for key in json_load_list:
        rows.append(key)

    return render_template('kakeibo_test_json/kd.html', title='kakeibo', pay_list=rows)

if __name__ == '__main__':
    app.run()


このブログを検索

アーカイブ

カテゴリー

QooQ