Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 80 |
-
"""検索結果をTXT
|
| 81 |
if not results:
|
| 82 |
-
return
|
| 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 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 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.
|
| 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 |
-
|
| 135 |
inputs=[result_table, search_condition_state],
|
| 136 |
-
outputs=[export_output
|
| 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})")
|