SG34 commited on
Commit
a595cf1
Β·
verified Β·
1 Parent(s): df49ce0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -18,17 +18,35 @@ def scrape_naver_stock_rise():
18
  for row in rows[2:]: # μƒμœ„ 2개의 rowλŠ” 헀더 및 빈 μΉΈμ΄λ―€λ‘œ μ œμ™Έ
19
  columns = row.find_all('td')
20
  if len(columns) > 1:
21
- rank = columns[0].text.strip()
22
  name = columns[1].text.strip()
23
- current_price = columns[2].text.strip()
24
- price_diff = columns[3].text.strip().replace('μƒν•œκ°€', '').replace('μƒμŠΉ', '').strip()
25
- change_rate = columns[4].text.strip()
26
- volume = columns[5].text.strip()
 
 
 
 
 
 
 
 
27
 
28
- stock_data.append([rank, name, current_price, price_diff, change_rate, volume])
 
 
 
 
 
 
 
 
 
 
29
 
30
  # DataFrame으둜 λ³€ν™˜
31
- df = pd.DataFrame(stock_data, columns=['μˆœμœ„', 'μ’…λͺ©λͺ…', 'ν˜„μž¬κ°€', '전일비', '등락λ₯ ', 'κ±°λž˜λŸ‰'])
32
  return df
33
 
34
  # μ—‘μ…€λ‘œ μ €μž₯ν•˜λŠ” ν•¨μˆ˜
@@ -42,12 +60,12 @@ def save_to_excel():
42
  def display_and_save_stocks():
43
  stock_data = scrape_naver_stock_rise()
44
  file_path = save_to_excel()
45
- return stock_data, file_path
46
 
47
  # Gradio UI μ„€μ •
48
  iface = gr.Interface(fn=display_and_save_stocks,
49
  inputs=[],
50
- outputs=[gr.Dataframe(label="μƒμŠΉ μ’…λͺ©"), gr.File(label="μ—‘μ…€ 파일 λ‹€μš΄λ‘œλ“œ")], # DataFrame 좜λ ₯κ³Ό μ—‘μ…€ 파일 λ‹€μš΄λ‘œλ“œ μΆ”κ°€
51
  title="넀이버 증ꢌ μƒμŠΉ μ’…λͺ© μŠ€ν¬λž˜ν•‘",
52
  description="넀이버 μ¦κΆŒμ—μ„œ ν˜„μž¬ μƒμŠΉ 쀑인 μƒμœ„ μ’…λͺ©μ„ μŠ€ν¬λž˜ν•‘ν•˜μ—¬ ν‘œλ‘œ 보여주고, μ—‘μ…€ 파일둜 μ €μž₯ν•©λ‹ˆλ‹€.")
53
  iface.launch()
 
18
  for row in rows[2:]: # μƒμœ„ 2개의 rowλŠ” 헀더 및 빈 μΉΈμ΄λ―€λ‘œ μ œμ™Έ
19
  columns = row.find_all('td')
20
  if len(columns) > 1:
21
+ rank = int(columns[0].text.strip())
22
  name = columns[1].text.strip()
23
+ current_price = int(columns[2].text.replace(",", "").strip())
24
+ price_diff = columns[3].text.strip().replace(",", "").replace('μƒν•œκ°€', '').replace('μƒμŠΉ', '').strip()
25
+ price_diff = int(price_diff) if price_diff else 0
26
+ change_rate = columns[4].text.strip().replace("%", "").replace(",", "").strip()
27
+ change_rate = float(change_rate) if change_rate else 0.0
28
+ volume = int(columns[5].text.replace(",", "").strip())
29
+ bid_price = int(columns[6].text.replace(",", "").strip())
30
+ ask_price = int(columns[7].text.replace(",", "").strip())
31
+ bid_total_volume = int(columns[8].text.replace(",", "").strip())
32
+ ask_total_volume = int(columns[9].text.replace(",", "").strip())
33
+ per = float(columns[10].text.strip()) if columns[10].text.strip() else None
34
+ roe = float(columns[11].text.strip()) if columns[11].text.strip() else None
35
 
36
+ # 가격 변동에 λ”°λ₯Έ 색상 ν‘œμ‹œ
37
+ if price_diff > 0:
38
+ price_diff = f"<span style='color:red;'>{price_diff}</span>"
39
+ change_rate = f"<span style='color:red;'>{change_rate}%</span>"
40
+ elif price_diff < 0:
41
+ price_diff = f"<span style='color:blue;'>{price_diff}</span>"
42
+ change_rate = f"<span style='color:blue;'>{change_rate}%</span>"
43
+ else:
44
+ change_rate = f"{change_rate}%"
45
+
46
+ stock_data.append([rank, name, current_price, price_diff, change_rate, volume, bid_price, ask_price, bid_total_volume, ask_total_volume, per, roe])
47
 
48
  # DataFrame으둜 λ³€ν™˜
49
+ df = pd.DataFrame(stock_data, columns=['μˆœμœ„', 'μ’…λͺ©λͺ…', 'ν˜„μž¬κ°€', '전일비', '등락λ₯ ', 'κ±°λž˜λŸ‰', 'λ§€μˆ˜ν˜Έκ°€', 'λ§€λ„ν˜Έκ°€', 'λ§€μˆ˜μ΄μž”λŸ‰', 'λ§€λ„μ΄μž”λŸ‰', 'PER', 'ROE'])
50
  return df
51
 
52
  # μ—‘μ…€λ‘œ μ €μž₯ν•˜λŠ” ν•¨μˆ˜
 
60
  def display_and_save_stocks():
61
  stock_data = scrape_naver_stock_rise()
62
  file_path = save_to_excel()
63
+ return stock_data.to_html(escape=False), file_path # escape=False둜 HTML νƒœκ·Έκ°€ μ μš©λ˜λ„λ‘ 함
64
 
65
  # Gradio UI μ„€μ •
66
  iface = gr.Interface(fn=display_and_save_stocks,
67
  inputs=[],
68
+ outputs=[gr.HTML(label="μƒμŠΉ μ’…λͺ©"), gr.File(label="μ—‘μ…€ 파일 λ‹€μš΄λ‘œλ“œ")], # HTML 좜λ ₯을 ν†΅ν•œ 색상 ν‘œμ‹œ 및 μ—‘μ…€ λ‹€μš΄λ‘œλ“œ
69
  title="넀이버 증ꢌ μƒμŠΉ μ’…λͺ© μŠ€ν¬λž˜ν•‘",
70
  description="넀이버 μ¦κΆŒμ—μ„œ ν˜„μž¬ μƒμŠΉ 쀑인 μƒμœ„ μ’…λͺ©μ„ μŠ€ν¬λž˜ν•‘ν•˜μ—¬ ν‘œλ‘œ 보여주고, μ—‘μ…€ 파일둜 μ €μž₯ν•©λ‹ˆλ‹€.")
71
  iface.launch()