Created
May 14, 2025 11:09
-
-
Save ochilab/fd50c4feadbac19a2eb1e566fe3bc6a8 to your computer and use it in GitHub Desktop.
GASから得たJSONをFletで受け取り表示する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import flet as ft | |
### Google Apps ScriptのURLを指定 | |
url="ここにGASのURL" | |
def fetch_data(value): | |
global url | |
url2=url+"?keyword=" + value | |
# Google Apps ScriptのURLを指定 | |
response = requests.get(url2) | |
print(response.status_code) | |
if response.status_code == 200: | |
print(response.text) | |
return response.json() | |
else: | |
print(f"Error: {response.status_code}") | |
return None | |
def main(page: ft.Page): | |
def on_click(e): | |
value = text_field.value | |
data = fetch_data(value) | |
if data: | |
#カウントして1行ずつ表示 | |
result_text.value = "" | |
for i in range(len(data)): | |
result_text.value += str(i+1) + "行目" + data[i]['name'] + "さんのデータが取得できました。\n" | |
else: | |
result_text.value = "データの取得に失敗しました。" | |
page.update() | |
##GUIコンポーネントの定義 | |
label =ft.Text("Flet 動作確認アプリ", size=24) | |
text_field = ft.TextField(label="検索キー を入力", value="001") | |
result_text = ft.Text(value="ここに結果が表示されます。") | |
fetch_button = ft.ElevatedButton(text="データを取得", on_click=on_click) | |
##GUIコンポーネントの配置 | |
page.add(label, text_field, fetch_button, result_text) | |
ft.app(target=main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment