-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
291 lines (247 loc) · 9.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from PyQt5.QtCore import QUrl, Qt
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QWidget, QGridLayout, QLabel, \
QSpacerItem, QSizePolicy, QComboBox, QLineEdit, QPushButton
from pyecharts import Bar, Pie, Line, Overlap
from pyecharts_javascripthon.api import TRANSLATOR
import requests
import json
import datetime as DT
yburl = 'https://api.qweather.com/v7/air/now?' #查找对应城市信息
aurl = 'https://geoapi.qweather.com/v2/city/lookup?' #查找cityid
history_url = 'https://datasetapi.qweather.com/v7/historical/air?'#查找历史天气
value = {
'location': '北京',
'key': '102d6f9e820f4db6bf59e89b4c39b276',
'lang': 'zh',
'date': '20211022'
}
TITLE_TEXT = "天气情况" # 主标题
TITLE_SUBTEXT = "过去七天" # 副标题
index = ["AQI", "pm10", "pm2p5", "no2", "no3", "co", "o3"]
week_ago = ["AQI", "pm10", "pm2p5", "no2", "no3", "co", "o3"]
history = []
class Form(QWidget):
def __init__(self):
super(Form, self).__init__()
self.view = None
self.echarts = False
self.initUi()
self.load_url()
def initUi(self):
self.hl = QHBoxLayout(self)
self.widget = QWidget()
self.gl = QGridLayout(self.widget)
#添加查询
nameLabel = QLabel("城市名称")
self.nameLineEdit = QLineEdit("")
self.gl.addWidget(nameLabel, 1 - 1, 0, 1, 1)
self.gl.addWidget(self.nameLineEdit, 2 - 1, 0, 1, 1)
save_Btn = QPushButton('查询')
save_Btn.clicked.connect(self.addNum)
self.gl.addWidget(save_Btn, 3 - 1, 0, 1, 1)
# ATTR1
self.label1 = QLabel(index[0] + ':' + AQI)
self.label1.setText(index[0] + ':' + AQI)
self.gl.addWidget(self.label1, 4 - 1, 0, 1, 1) #向布局中添加内容
# ATTR2
self.label2 = QLabel(index[1] + ':' + pm10)
self.label2.setText(index[1] + ':' + pm10)
self.gl.addWidget(self.label2, 5 - 1, 0, 1, 1)
# ATTR3
self.label3 = QLabel(index[2] + ':' + pm2p5)
self.label3.setText(index[2] + ':' + pm2p5)
self.gl.addWidget(self.label3, 6 - 1, 0, 1, 1)
# ATTR4
self.label4 = QLabel(index[3] + ':' + no2)
self.label4.setText(index[3] + ':' + no2)
self.gl.addWidget(self.label4, 7 - 1, 0, 1, 1)
# ATTR5
self.label5 = QLabel(index[4] + ':' + so3)
self.label5.setText(index[4] + ':' + so3)
self.gl.addWidget(self.label5, 8 - 1, 0, 1, 1)
# ATTR6
self.label6 = QLabel(index[5] + ':' + co)
self.label6.setText(index[5] + ':' + co)
self.gl.addWidget(self.label6, 9 - 1, 0, 1, 1)
# ATTR7
self.label7 = QLabel(index[6] + ':' + o3)
self.label7.setText(index[6] + ':' + o3)
self.gl.addWidget(self.label7, 10 - 1, 0, 1, 1)
self.hl.addWidget(self.widget)
vs = QSpacerItem(15, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.gl.addItem(vs, 11, 0, 1, 2)
self.combobox_type = QComboBox()
self.combobox_type.currentIndexChanged.connect(self.reload_canvas)
self.combobox_type.addItems(['饼图', '柱状图', '折线图', '折线、柱状图'])
self.gl.addWidget(self.combobox_type, 12, 0, 1, 2)
self.combobox_theme = QComboBox()
self.combobox_theme.currentTextChanged.connect(self.change_theme)
self.combobox_theme.addItems(['light', 'dark'])
self.gl.addWidget(self.combobox_theme, 13, 0, 1, 2)
# 添加web view
self.view = QWebEngineView()
self.view.setContextMenuPolicy(Qt.NoContextMenu)
self.hl.addWidget(self.view)
def addNum(self):
city = self.nameLineEdit.text()
location = city
value['location'] = location
#print(value)
areq_2 = requests.get(aurl, params=value)
cityid = areq_2.json()['location'][0]['id']
value['location'] = cityid
#print(cityid)
ajs_2 = requests.get(yburl, params=value).json() # ajs是当下的天气情况
#print(ajs_2['now']['aqi'])
self.label1.setText(index[0]+' : '+ajs_2['now']['aqi'])
self.label2.setText(index[1]+' : '+ajs_2['now']['pm10'])
self.label3.setText(index[2]+' : '+ajs_2['now']['pm2p5'])
self.label4.setText(index[3]+' : '+ajs_2['now']['no2'])
self.label5.setText(index[4]+' : '+ajs_2['now']['so2'])
self.label6.setText(index[5]+' : '+ajs_2['now']['co'])
self.label7.setText(index[6]+' : '+ajs_2['now']['o3'])
global history
history = []
for i in range(0, 7):
value['date'] = week_ago[i]
history_js = requests.get(history_url, params=value).json()
# print(json.dumps(history_js, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False))
# print(history_js['airHourly'][0]['aqi'])
history.append(history_js['airHourly'][0]['aqi'])
self.reload_canvas()
def change_theme(self, theme):
if not self.view:
return
options = self.get_options()
if not options:
return
self.view.page().runJavaScript(
f'''
myChart.dispose();
var myChart = echarts.init(document.getElementById('container'), '{theme}', {{renderer: 'canvas'}});
myChart.clear();
var option = eval({options});
myChart.setOption(option);
'''
)
def load_url(self):
url = QUrl("file:///template.html")
self.view.load(url)
self.view.loadFinished.connect(self.set_options)
def reload_canvas(self):
if not self.view:
return
# 重载画布
options = self.get_options()
if not options:
return
self.view.page().runJavaScript(
f'''
myChart.clear();
var option = eval({options});
myChart.setOption(option);
'''
)
def set_options(self):
if not self.view:
return
if not self.echarts:
# 初始化echarts
self.view.page().runJavaScript(
'''
var myChart = echarts.init(document.getElementById('container'), 'light', {renderer: 'canvas'});
'''
)
self.echarts = True
options = self.get_options()
if not options:
return
self.view.page().runJavaScript(
f'''
var option = eval({options});
myChart.setOption(option);
'''
)
def get_options(self):
v = history
if self.combobox_type.currentIndex() == 0:
# 饼图
options = self.create_pie(v)
elif self.combobox_type.currentIndex() == 1:
# 柱状图
options = self.create_bar(v)
elif self.combobox_type.currentIndex() == 2:
# 折线图
options = self.create_line(v)
elif self.combobox_type.currentIndex() == 3:
# 折线、柱状图
options = self.create_line_bar(v)
else:
return
return options
def create_pie(self, v):
self.pie = Pie(TITLE_TEXT, TITLE_SUBTEXT)
self.pie.add("天气AQI情况", ATTR, v, is_label_show=True)
snippet = TRANSLATOR.translate(self.pie.options)
options = snippet.as_snippet()
return options
def create_bar(self, v):
self.bar = Bar(TITLE_TEXT, TITLE_SUBTEXT)
self.bar.add('过去七天的天气AQI', ATTR, v, is_more_utils=True)
snippet = TRANSLATOR.translate(self.bar.options)
options = snippet.as_snippet()
return options
def create_line(self, v):
self.line = Line(TITLE_TEXT, TITLE_SUBTEXT)
self.line.add("天气AQI", ATTR, v, is_smooth=True, mark_line=["max", "average"])
snippet = TRANSLATOR.translate(self.line.options)
options = snippet.as_snippet()
return options
def create_line_bar(self, v):
today = DT.date.today()
week_ago = ["20" + (today - DT.timedelta(days=i)).strftime("%y%m%d") for i in range(1, 8)]
week_ago.reverse()
line = Line(TITLE_TEXT, TITLE_SUBTEXT)
line.add("天气", ATTR, v, is_smooth=True, mark_line=["max", "average"])
bar = Bar(TITLE_TEXT, TITLE_SUBTEXT)
bar.add('天气', ATTR, v, is_more_utils=True)
overlap = Overlap()
overlap.add(line)
overlap.add(bar)
snippet = TRANSLATOR.translate(overlap.options)
options = snippet.as_snippet()
return options
if __name__ == '__main__':
import sys
today = DT.date.today()
ATTR_2 = ["20" + (today - DT.timedelta(days=i)).strftime("%y%m%d") for i in range(1, 8)]
ATTR_2.reverse()
ATTR = [(today - DT.timedelta(days=i)).strftime("%y%m%d")[2:] for i in range(1, 8)]
ATTR.reverse()
location = 'beijing'
value['location'] = location
areq = requests.get(aurl, params=value)
cityid = areq.json()['location'][0]['id']
value['location'] = cityid
ajs = requests.get(yburl, params=value).json() # ajs是当下的天气情况
AQI = ajs['now']['aqi']
pm10 = ajs['now']['pm10']
pm2p5 = ajs['now']['pm2p5']
no2 = ajs['now']['no2']
so3 = ajs['now']['so2']
co = ajs['now']['co']
o3 = ajs['now']['o3']
week_ago = ["20" + (today - DT.timedelta(days=i)).strftime("%y%m%d") for i in range(1, 8)]
week_ago.reverse()
for i in range(0, 7):
value['date'] = week_ago[i]
history_js = requests.get(history_url, params=value).json()
# print(json.dumps(history_js, sort_keys=True, indent=4, separators=(', ', ': '), ensure_ascii=False))
#print(history_js['airHourly'][0]['aqi'])
history.append(history_js['airHourly'][0]['aqi'])
app = QApplication(sys.argv)
app.setStyle('fusion')
form_click = Form()
form_click.show()
sys.exit(app.exec_())