JAMESPARK3 commited on
Commit
fef2b07
Β·
verified Β·
1 Parent(s): c68dafc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -31
app.py CHANGED
@@ -203,33 +203,37 @@ def check_network_status():
203
  def get_weather_data():
204
  url = "http://openapi.seoul.go.kr:8088/77544e69764a414d363647424a655a/xml/citydata/1/5/μ‹ λ¦Όμ—­"
205
  try:
206
- response = requests.get(url, timeout=10)
207
  response.raise_for_status()
208
 
 
 
 
 
209
  response_text = response.text.strip()
210
  if not response_text:
211
- st.session_state.api_failed = True
212
  st.error("응닡이 λΉ„μ–΄ μžˆμŠ΅λ‹ˆλ‹€.")
213
  return None
214
 
 
 
 
215
  data = xmltodict.parse(response_text)
216
  result = data['SeoulRtd.citydata']['CITYDATA']['WEATHER_STTS']['WEATHER_STTS']
217
 
218
- st.session_state.api_failed = False
 
 
219
  return result
220
 
221
  except requests.exceptions.Timeout:
222
- st.session_state.api_failed = True
223
  st.error("API μš”μ²­ μ‹œκ°„μ΄ μ΄ˆκ³Όλ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
224
  except requests.exceptions.RequestException as e:
225
- st.session_state.api_failed = True
226
  st.error(f"API 호좜 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
227
  except Exception as e:
228
- st.session_state.api_failed = True
229
  st.error(f"데이터 처리 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
230
  return None
231
 
232
-
233
  def get_background_color(pm10_value):
234
  try:
235
  pm10 = float(pm10_value)
@@ -302,22 +306,12 @@ def show_weather_info(data):
302
  st.markdown('<div class="section-container">', unsafe_allow_html=True)
303
 
304
  # Add update time display using the last API call timestamp (already in KST)
305
- # show_weather_info ν•¨μˆ˜ λ‚΄μ˜ refresh time ν‘œμ‹œ 뢀뢄을 μˆ˜μ •
306
  refresh_time = datetime.fromtimestamp(st.session_state.last_api_call) if st.session_state.last_api_call else (datetime.utcnow() + timedelta(hours=9))
307
-
308
- if st.session_state.api_failed:
309
- st.markdown(f'''
310
- <div style="text-align: center; font-size: 0.8em; color: red;">
311
- API failed: {refresh_time.strftime('%Y-%m-%d %H:%M')}
312
- </div>
313
- ''', unsafe_allow_html=True)
314
- else:
315
- st.markdown(f'''
316
- <div style="text-align: center; font-size: 0.8em; color: gray;">
317
- Data refreshed at: {refresh_time.strftime('%Y-%m-%d %H:%M:%S')}
318
- </div>
319
- ''', unsafe_allow_html=True)
320
-
321
 
322
  # Add this code to define formatted_date
323
  current_time = datetime.utcnow() + timedelta(hours=9)
@@ -761,9 +755,6 @@ def main():
761
  st.session_state.current_section = 'weather'
762
  st.session_state.last_api_call = 0
763
  st.session_state.weather_data = None
764
- st.session_state.api_failed = False # 여기에 api_failed μ΄ˆκΈ°ν™” μΆ”κ°€
765
-
766
-
767
 
768
  # ν˜„μž¬ μ‹œκ°„μ„ μ„œμšΈ μ‹œκ°„μœΌλ‘œ κ°€μ Έμ˜΅λ‹ˆλ‹€.
769
  current_time = datetime.utcnow() + timedelta(hours=9)
@@ -786,16 +777,11 @@ def main():
786
  if new_data: # μƒˆ 데이터λ₯Ό μ„±κ³΅μ μœΌλ‘œ 받아왔을 λ•Œλ§Œ μ—…λ°μ΄νŠΈ
787
  st.session_state.weather_data = new_data
788
  st.session_state.last_api_call = current_timestamp
789
- st.session_state.api_failed = False
790
- st.rerun()
791
  except Exception as e:
792
  st.error(f"Failed to refresh data: {str(e)}")
793
- st.session_state.api_failed = True
794
  else:
795
  st.warning("ν˜„μž¬ λ„€νŠΈμ›Œν¬μ— λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. 데이터 갱신이 λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€.")
796
- st.session_state.api_failed = True
797
- if st.session_state.weather_data: # 이전 데이터가 있으면 계속 μ‚¬μš©
798
- data = st.session_state.weather_data
799
 
800
  data = st.session_state.weather_data
801
  if data:
 
203
  def get_weather_data():
204
  url = "http://openapi.seoul.go.kr:8088/77544e69764a414d363647424a655a/xml/citydata/1/5/μ‹ λ¦Όμ—­"
205
  try:
206
+ response = requests.get(url, timeout=10) # timeout μΆ”κ°€
207
  response.raise_for_status()
208
 
209
+ # 응닡 λ‚΄μš© 디버깅
210
+ st.write("Status Code:", response.status_code)
211
+ st.write("Response Headers:", dict(response.headers))
212
+
213
  response_text = response.text.strip()
214
  if not response_text:
 
215
  st.error("응닡이 λΉ„μ–΄ μžˆμŠ΅λ‹ˆλ‹€.")
216
  return None
217
 
218
+ # XML νŒŒμ‹± μ „ 응닡 확인
219
+ st.write("Raw Response:", response_text[:200]) # 처음 200자만 좜λ ₯
220
+
221
  data = xmltodict.parse(response_text)
222
  result = data['SeoulRtd.citydata']['CITYDATA']['WEATHER_STTS']['WEATHER_STTS']
223
 
224
+ # νŒŒμ‹±λœ 데이터 확인
225
+ st.write("Parsed Data:", result)
226
+
227
  return result
228
 
229
  except requests.exceptions.Timeout:
 
230
  st.error("API μš”μ²­ μ‹œκ°„μ΄ μ΄ˆκ³Όλ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
231
  except requests.exceptions.RequestException as e:
 
232
  st.error(f"API 호좜 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
233
  except Exception as e:
 
234
  st.error(f"데이터 처리 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
235
  return None
236
 
 
237
  def get_background_color(pm10_value):
238
  try:
239
  pm10 = float(pm10_value)
 
306
  st.markdown('<div class="section-container">', unsafe_allow_html=True)
307
 
308
  # Add update time display using the last API call timestamp (already in KST)
 
309
  refresh_time = datetime.fromtimestamp(st.session_state.last_api_call) if st.session_state.last_api_call else (datetime.utcnow() + timedelta(hours=9))
310
+ st.markdown(f'''
311
+ <div style="text-align: center; font-size: 0.8em; color: gray;">
312
+ Data refreshed at: {refresh_time.strftime('%Y-%m-%d %H:%M:%S')}
313
+ </div>
314
+ ''', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
315
 
316
  # Add this code to define formatted_date
317
  current_time = datetime.utcnow() + timedelta(hours=9)
 
755
  st.session_state.current_section = 'weather'
756
  st.session_state.last_api_call = 0
757
  st.session_state.weather_data = None
 
 
 
758
 
759
  # ν˜„μž¬ μ‹œκ°„μ„ μ„œμšΈ μ‹œκ°„μœΌλ‘œ κ°€μ Έμ˜΅λ‹ˆλ‹€.
760
  current_time = datetime.utcnow() + timedelta(hours=9)
 
777
  if new_data: # μƒˆ 데이터λ₯Ό μ„±κ³΅μ μœΌλ‘œ 받아왔을 λ•Œλ§Œ μ—…λ°μ΄νŠΈ
778
  st.session_state.weather_data = new_data
779
  st.session_state.last_api_call = current_timestamp
780
+ st.rerun() # νŽ˜μ΄μ§€ μƒˆλ‘œκ³ μΉ¨
 
781
  except Exception as e:
782
  st.error(f"Failed to refresh data: {str(e)}")
 
783
  else:
784
  st.warning("ν˜„μž¬ λ„€νŠΈμ›Œν¬μ— λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. 데이터 갱신이 λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€.")
 
 
 
785
 
786
  data = st.session_state.weather_data
787
  if data: