xzg82465 commited on
Commit
aefc716
·
verified ·
1 Parent(s): 85c652b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -23
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- import subprocess
3
 
4
  import numpy as np
5
  import matplotlib.pyplot as plt
@@ -54,28 +54,18 @@ def run_all(tickersymbol, download_data, download_update, use_train, method, PRE
54
  yield
55
 
56
 
57
- def run_command():
58
- # 這裡以執行一段簡單的 ping 指令或 python 腳本為例
59
- # 你可以替換成你實際要在 container 內執行的指令
60
- command = ["python", "-c", "import time; print('Starting...'); time.sleep(1); print('Running...'); time.sleep(1); print('Done!')"]
61
 
62
- # 使用 Popen 捕捉 stdout 和 stderr
63
- process = subprocess.Popen(
64
- command,
65
- stdout=subprocess.PIPE,
66
- stderr=subprocess.STDOUT, # 將錯誤訊息導向標準輸出
67
- text=True, # 將輸出解碼為字串
68
- bufsize=1 # 行緩衝 (Line buffered)
69
- )
70
-
71
- full_output = ""
72
- # 逐行讀取輸出並即時回傳給 Gradio
73
- for line in iter(process.stdout.readline, ''):
74
- full_output += line
75
- yield full_output # yield 會即時更新前端的 Textbox
76
 
77
- process.stdout.close()
78
- process.wait()
79
 
80
 
81
  def update_setup(is_checked):
@@ -113,11 +103,11 @@ def update_ticker_list(market_choice):
113
  return gr.update(choices=["No tickers loaded"], value="No tickers loaded")
114
 
115
  symbol_list = ["No tickers loaded"]
116
-
117
  today = datetime.date.today()
118
  yesterday = today - datetime.timedelta(days=1)
119
  tomorow = today + datetime.timedelta(days=1)
120
 
 
121
  with gr.Blocks() as demo:
122
  gr.Markdown("### 股票分析與訓練設定")
123
 
@@ -170,6 +160,7 @@ with gr.Blocks() as demo:
170
 
171
  with gr.Row():
172
  run_btn = gr.Button("啟動")
 
173
 
174
  ##--觸發事件--
175
  market_radio.change(
@@ -214,7 +205,7 @@ with gr.Blocks() as demo:
214
  outputs = [config_display] # 這裡依序對應 yield 的回傳值
215
  )#執行按鈕的邏輯
216
  run_btn.click(
217
- fn=run_command,
218
  inputs=[],
219
  outputs=console_output
220
  )
 
1
  import gradio as gr
2
+ from contextlib import redirect_stdout
3
 
4
  import numpy as np
5
  import matplotlib.pyplot as plt
 
54
  yield
55
 
56
 
57
+ def run_python_code():
58
+ # 建立一個字串 IO 來接收輸出
59
+ f = io.StringIO()
 
60
 
61
+ with redirect_stdout(f):
62
+ # 這裡放你原本會 print 東西的程式碼
63
+ print("載入模型中...")
64
+ print("處理資料...")
65
+ print("執行完成!")
 
 
 
 
 
 
 
 
 
66
 
67
+ # 獲取所有被捕捉到的輸出
68
+ return f.getvalue()
69
 
70
 
71
  def update_setup(is_checked):
 
103
  return gr.update(choices=["No tickers loaded"], value="No tickers loaded")
104
 
105
  symbol_list = ["No tickers loaded"]
 
106
  today = datetime.date.today()
107
  yesterday = today - datetime.timedelta(days=1)
108
  tomorow = today + datetime.timedelta(days=1)
109
 
110
+
111
  with gr.Blocks() as demo:
112
  gr.Markdown("### 股票分析與訓練設定")
113
 
 
160
 
161
  with gr.Row():
162
  run_btn = gr.Button("啟動")
163
+ console_output = gr.Textbox(label="Console Output", lines=5, interactive=False)
164
 
165
  ##--觸發事件--
166
  market_radio.change(
 
205
  outputs = [config_display] # 這裡依序對應 yield 的回傳值
206
  )#執行按鈕的邏輯
207
  run_btn.click(
208
+ fn=run_python_code,
209
  inputs=[],
210
  outputs=console_output
211
  )