2catycm commited on
Commit
76afe6d
·
1 Parent(s): f072311

feat: auto download

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -3,15 +3,18 @@ TODO
3
  """
4
  # 首先计算相对路径
5
  from pathlib import Path
 
 
 
 
6
  this_file = Path(__file__).resolve()
7
  this_directory = this_file.parent
 
8
  data_cangzhou_folder = this_directory / "data/Cangzhou"
9
  data_static_folder = data_cangzhou_folder / "static"
10
 
11
- print(f"当前文件路径:{this_file}")
12
- print(f"当前文件夹路径:{this_directory}")
13
- print(f"ls .: {os.listdir(this_directory)}")
14
- print(f"ls data/Cangzhou: {os.listdir(data_cangzhou_folder)}")
15
 
16
  # 数据文件夹路径(静态数据)
17
  data_folder = data_static_folder
@@ -19,18 +22,32 @@ data_folder = data_static_folder
19
  # 然后
20
  import streamlit as st
21
  import pandas as pd
22
- import os
23
  from datetime import datetime
24
-
25
  import plotly.graph_objects as go
26
  import plotly.express as px
27
 
28
  # 设置页面配置为宽屏模式,以便能同时显示三个图表
29
  st.set_page_config(layout="wide")
30
 
 
 
 
31
  # 设置应用标题,参考数据集介绍,反映作业要求
32
  st.title("S&M-HSTPM2d5数据集可视化——清华大学数据可视化课程作业1")
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # 创建三个等宽的列,分别展示图(a)、图(b)和图(c)
35
  col1, col2, col3 = st.columns(3)
36
 
@@ -51,7 +68,7 @@ with col1:
51
  st.markdown("1. 使用 static 文件夹中的 .csv 文件,并筛选出时间戳在 2019-01-01 00:00:00 到 2019-01-01 12:00:00 之间的数据。")
52
  st.markdown("2. X 轴和 Y 轴分别表示时间和 PM2.5 浓度水平。")
53
  st.markdown("3. 为每个静态传感器绘制一条折线,并用不同颜色进行区分。")
54
-
55
  # 定义过滤时间段
56
  start_dt = datetime(2019, 1, 1, 0, 0, 0)
57
  end_dt = datetime(2019, 1, 1, 12, 0, 0)
@@ -75,7 +92,7 @@ with col1:
75
  labels={"variable": "传感器", "value": "PM2.5", "index": "时间"})
76
  fig_line.update_layout(xaxis_title="时间", yaxis_title="PM2.5水平")
77
  st.plotly_chart(fig_line, use_container_width=True)
78
-
79
  with st.expander("详细说明"):
80
  st.markdown("**描述:** 本图展示了各静态传感器在2019年1月1日0:00至12:00期间的PM2.5浓度随时间变化的趋势。横轴表示时间,纵轴表示PM2.5数值,不同折线代表不同传感器的数据。")
81
  st.markdown("**解读:** 曲线波动反映了空气质量的时段变化,峰值可能预示短期污染事件,而持续低值表明空气较为清洁。传感器数据对比有助于区域污染差异的分析。")
@@ -144,7 +161,6 @@ with col2:
144
  st.markdown("**描述:** 本图利用Mobile文件夹中的CSV数据,在2019年1月2日10:00至10:20期间展示车辆传感器的位置分布。横轴表示经度,纵轴表示纬度,不同颜色代表不同传感器。")
145
  st.markdown("**解读:** 通过调整散点透明度(早期数据较透明),图中显示了车辆移动的时间演变趋势,为探索城市中车辆行驶路径提供依据。")
146
 
147
-
148
  # 图(c): 3D直方图——展示整个城市在特定时段内的PM2.5分布
149
  with col3:
150
  st.header("(c)3D直方图:展示整个城市在特定时段内的PM2.5分布")
@@ -152,7 +168,7 @@ with col3:
152
  st.markdown("1. 使用 mobile 和 static 文件夹中的 .csv 文件,并筛选出时间戳在 2019-01-01 09:00:00 到 2019-01-01 09:10:00 之间的数据。")
153
  st.markdown("2. 将数据的GPS坐标转换为网格坐标(例如采用 0.01 度的分辨率),并在相同网格内聚合数据。")
154
  st.markdown("3. X 轴、Y 轴和 Z 轴分别表示经度、纬度和PM2.5数值。")
155
-
156
  # 定义过滤时间段
157
  start_c = datetime(2019, 1, 1, 9, 0, 0)
158
  end_c = datetime(2019, 1, 1, 9, 10, 0)
 
3
  """
4
  # 首先计算相对路径
5
  from pathlib import Path
6
+ import os
7
+ import urllib.request, zipfile
8
+ import streamlit as st
9
+
10
  this_file = Path(__file__).resolve()
11
  this_directory = this_file.parent
12
+ # 数据集文件夹路径
13
  data_cangzhou_folder = this_directory / "data/Cangzhou"
14
  data_static_folder = data_cangzhou_folder / "static"
15
 
16
+
17
+
 
 
18
 
19
  # 数据文件夹路径(静态数据)
20
  data_folder = data_static_folder
 
22
  # 然后
23
  import streamlit as st
24
  import pandas as pd
 
25
  from datetime import datetime
 
26
  import plotly.graph_objects as go
27
  import plotly.express as px
28
 
29
  # 设置页面配置为宽屏模式,以便能同时显示三个图表
30
  st.set_page_config(layout="wide")
31
 
32
+
33
+
34
+
35
  # 设置应用标题,参考数据集介绍,反映作业要求
36
  st.title("S&M-HSTPM2d5数据集可视化——清华大学数据可视化课程作业1")
37
 
38
+
39
+ # 如果 data/Cangzhou 目录不存在,则下载并解压数据集
40
+ if not (data_cangzhou_folder).exists():
41
+ st.info("Cangzhou 数据集不存在,正在下载中,请耐心等待……")
42
+ url = "https://zenodo.org/records/4028130/files/Cangzhou.zip?download=1"
43
+ local_zip = this_directory / "Cangzhou.zip"
44
+ urllib.request.urlretrieve(url, str(local_zip))
45
+ with zipfile.ZipFile(str(local_zip), "r") as zip_ref:
46
+ zip_ref.extractall(path=str(this_directory / "data"))
47
+ st.success("Cangzhou 数据集下载并解压完成!")
48
+ st.rerun()
49
+
50
+ st.markdown("叶璨铭,2024214500,[email protected]")
51
  # 创建三个等宽的列,分别展示图(a)、图(b)和图(c)
52
  col1, col2, col3 = st.columns(3)
53
 
 
68
  st.markdown("1. 使用 static 文件夹中的 .csv 文件,并筛选出时间戳在 2019-01-01 00:00:00 到 2019-01-01 12:00:00 之间的数据。")
69
  st.markdown("2. X 轴和 Y 轴分别表示时间和 PM2.5 浓度水平。")
70
  st.markdown("3. 为每个静态传感器绘制一条折线,并用不同颜色进行区分。")
71
+
72
  # 定义过滤时间段
73
  start_dt = datetime(2019, 1, 1, 0, 0, 0)
74
  end_dt = datetime(2019, 1, 1, 12, 0, 0)
 
92
  labels={"variable": "传感器", "value": "PM2.5", "index": "时间"})
93
  fig_line.update_layout(xaxis_title="时间", yaxis_title="PM2.5水平")
94
  st.plotly_chart(fig_line, use_container_width=True)
95
+
96
  with st.expander("详细说明"):
97
  st.markdown("**描述:** 本图展示了各静态传感器在2019年1月1日0:00至12:00期间的PM2.5浓度随时间变化的趋势。横轴表示时间,纵轴表示PM2.5数值,不同折线代表不同传感器的数据。")
98
  st.markdown("**解读:** 曲线波动反映了空气质量的时段变化,峰值可能预示短期污染事件,而持续低值表明空气较为清洁。传感器数据对比有助于区域污染差异的分析。")
 
161
  st.markdown("**描述:** 本图利用Mobile文件夹中的CSV数据,在2019年1月2日10:00至10:20期间展示车辆传感器的位置分布。横轴表示经度,纵轴表示纬度,不同颜色代表不同传感器。")
162
  st.markdown("**解读:** 通过调整散点透明度(早期数据较透明),图中显示了车辆移动的时间演变趋势,为探索城市中车辆行驶路径提供依据。")
163
 
 
164
  # 图(c): 3D直方图——展示整个城市在特定时段内的PM2.5分布
165
  with col3:
166
  st.header("(c)3D直方图:展示整个城市在特定时段内的PM2.5分布")
 
168
  st.markdown("1. 使用 mobile 和 static 文件夹中的 .csv 文件,并筛选出时间戳在 2019-01-01 09:00:00 到 2019-01-01 09:10:00 之间的数据。")
169
  st.markdown("2. 将数据的GPS坐标转换为网格坐标(例如采用 0.01 度的分辨率),并在相同网格内聚合数据。")
170
  st.markdown("3. X 轴、Y 轴和 Z 轴分别表示经度、纬度和PM2.5数值。")
171
+
172
  # 定义过滤时间段
173
  start_c = datetime(2019, 1, 1, 9, 0, 0)
174
  end_c = datetime(2019, 1, 1, 9, 10, 0)