SPARC64 commited on
Commit
c516766
·
verified ·
1 Parent(s): 9d605a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import re
3
  import os
 
4
  from datetime import datetime, timedelta, timezone
5
 
6
  # 設定
@@ -76,28 +77,25 @@ def extract_date_range_logs(lines, start_date, end_date):
76
 
77
  return results
78
 
79
- def export_results(results, search_condition):
80
- """検索結果をTXTファイルとしてエクスポート"""
81
  if not results:
82
- return None, "エラー: エクスポートするデータがありません。"
83
-
84
- filename = f"search_results_{datetime.now(JST).strftime('%Y%m%d_%H%M%S')}.txt"
85
- file_path = os.path.join("/tmp", filename)
86
 
87
  execution_date = datetime.now(JST).strftime("%Y/%m/%d %H:%M:%S")
88
 
89
- try:
90
- with open(file_path, "w", encoding="utf-8") as f:
91
- f.write("プログラム名: LINEログマネージャー\n")
92
- f.write(f"検索実行日: {execution_date}\n")
93
- f.write(f"{search_condition}\n")
94
- f.write(f"ヒット件数: {len(results.splitlines())} 件\n")
95
- f.write("-" * 40 + "\n")
96
- f.write(results + "\n")
97
-
98
- return file_path, "エクスポート完了"
99
- except Exception as e:
100
- return None, f"エラー: {str(e)}"
101
 
102
  # Gradio UI
103
  with gr.Blocks() as demo:
@@ -121,7 +119,7 @@ with gr.Blocks() as demo:
121
 
122
  # エクスポート機能
123
  export_button = gr.Button("TXTをエクスポートする")
124
- export_output = gr.File(label="結果をダウンロード", interactive=True) # "ファイル名.txt" ではなく固定
125
  export_status = gr.Textbox(label="エクスポートメッセージ", interactive=False)
126
 
127
  search_button.click(
@@ -131,9 +129,9 @@ with gr.Blocks() as demo:
131
  )
132
 
133
  export_button.click(
134
- export_results,
135
  inputs=[result_table, search_condition_state],
136
- outputs=[export_output, export_status]
137
  )
138
 
139
  gr.Markdown(f"[📖 ヘルプページ]({SUPPORT_URL})")
 
1
  import gradio as gr
2
  import re
3
  import os
4
+ import base64
5
  from datetime import datetime, timedelta, timezone
6
 
7
  # 設定
 
77
 
78
  return results
79
 
80
+ def generate_download_link(results, search_condition):
81
+ """検索結果をTXTファイルとしてbase64エンコードし、ダウンロードリンクを生成"""
82
  if not results:
83
+ return "エラー: エクスポートするデータがありません。"
 
 
 
84
 
85
  execution_date = datetime.now(JST).strftime("%Y/%m/%d %H:%M:%S")
86
 
87
+ file_content = (
88
+ "プログラム名: LINEログマネージャー\n"
89
+ f"検索実行日: {execution_date}\n"
90
+ f"{search_condition}\n"
91
+ f"ヒット件数: {len(results.splitlines())} 件\n"
92
+ + "-" * 40 + "\n"
93
+ + results + "\n"
94
+ )
95
+
96
+ encoded_file = base64.b64encode(file_content.encode()).decode()
97
+ href = f'<a href="data:text/plain;base64,{encoded_file}" download="search_results.txt">結果をダウンロード</a>'
98
+ return href
99
 
100
  # Gradio UI
101
  with gr.Blocks() as demo:
 
119
 
120
  # エクスポート機能
121
  export_button = gr.Button("TXTをエクスポートする")
122
+ export_output = gr.HTML() # base64エンコードしたリンクを表示
123
  export_status = gr.Textbox(label="エクスポートメッセージ", interactive=False)
124
 
125
  search_button.click(
 
129
  )
130
 
131
  export_button.click(
132
+ generate_download_link,
133
  inputs=[result_table, search_condition_state],
134
+ outputs=[export_output]
135
  )
136
 
137
  gr.Markdown(f"[📖 ヘルプページ]({SUPPORT_URL})")