Python运用webbrowser打开PyEcharts生成的html文件

网友投稿 907 2022-05-29

话不多说,直接上代码

这里 也有更多pyecharts的代码~

示例

调用库

import pandas as pd import time from functools import partial from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui, QtWidgets from pyecharts import options as opts from pyecharts.charts import Kline, Line, Bar, Grid import webbrowser as wb

1

Python运用webbrowser打开PyEcharts生成的html文件

2

3

4

5

6

7

8

K线图、输出在默认浏览器显示

# 移动平均数计算 def moving_average(data, day_count): data = data.values[:, 0] result = [] for i in range(len(data)): start_day_index = i - day_count + 1 if start_day_index <= 0: start_day_index = 0 justified_day_count = i - start_day_index + 1 mean = data[start_day_index:i + 1].sum() / justified_day_count result.append(mean) return result # k线 --项目需求:已实现-- def show_kline(csv_name): # 读取.csv文件, stock_code = 'Brent_OIL' stock_data = pd.read_csv(csv_name, encoding='gb2312') # 将文件内容按照by=[‘date’]内容进行排序 stock_data = stock_data.sort_values(by=["date"], ascending=[True], inplace=False) stock_data_cleared = stock_data[stock_data['close'] > 0] stock_name = stock_data_cleared["position"][0] stock_data_extracted = stock_data_cleared[["open", "close", "low", "high", "volume", "date"]] kline = ( Kline() .add_xaxis(stock_data_extracted["date"].values.tolist()) .add_yaxis("K线图", stock_data_extracted.iloc[:, :4].values.tolist()) .set_global_opts( xaxis_opts=opts.AxisOpts(is_scale=True, is_show=False), # axis_opts=opts.AxisOpts(is_scale=True,min_=0), #y轴起始坐标可以设为0 yaxis_opts=opts.AxisOpts(is_scale=True), # y轴起始坐标可自动调整 #title_opts=opts.TitleOpts(title="价格", subtitle=stock_name + "\n" + stock_code, pos_top="20%"), axispointer_opts=opts.AxisPointerOpts( is_show=True, link=[{"xAxisIndex": "all"}], label=opts.LabelOpts(background_color="#777"), ), datazoom_opts=[ # 设置zoom参数后即可缩放 opts.DataZoomOpts( is_show=True, type_="inside", xaxis_index=[0, 1], # 设置第0轴和第1轴同时缩放 range_start=0, range_end=100, ), opts.DataZoomOpts( is_show=True, xaxis_index=[0, 1], type_="slider", pos_top="90%", range_start=0, range_end=100, ), ], ) ) # 移动平均线 line = ( Line() .add_xaxis(xaxis_data=stock_data_extracted["date"].values.tolist()) .add_yaxis( series_name="MA5", y_axis=moving_average(stock_data_extracted[["close"]], 5), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA10", y_axis=moving_average(stock_data_extracted[["close"]], 10), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA30", y_axis=moving_average(stock_data_extracted[["close"]], 30), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA60", y_axis=moving_average(stock_data_extracted[["close"]], 60), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA120", y_axis=moving_average(stock_data_extracted[["close"]], 120), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA240", y_axis=moving_average(stock_data_extracted[["close"]], 240), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .add_yaxis( series_name="MA360", y_axis=moving_average(stock_data_extracted[["close"]], 360), is_smooth=True, is_hover_animation=False, linestyle_opts=opts.LineStyleOpts(width=1, opacity=0.5), label_opts=opts.LabelOpts(is_show=False), ) .set_global_opts(xaxis_opts=opts.AxisOpts(type_="category")) ) # 将K线图和移动平均线显示在一个图内 kline.overlap(line) # 成交量柱形图 x = stock_data_extracted[["date"]].values[:, 0].tolist() y = stock_data_extracted[["volume"]].values[:, 0].tolist() bar = ( Bar() .add_xaxis(x) .add_yaxis("成交量", y, label_opts=opts.LabelOpts(is_show=False), itemstyle_opts=opts.ItemStyleOpts(color="#008080")) .set_global_opts(title_opts=opts.TitleOpts(title="成交量", pos_top="70%"), legend_opts=opts.LegendOpts(is_show=False), ) ) # 使用网格将多张图标组合到一起显示 grid_chart = Grid() grid_chart.add( kline, grid_opts=opts.GridOpts(pos_left="15%", pos_right="8%", height="55%"), ) grid_chart.add( bar, grid_opts=opts.GridOpts(pos_left="15%", pos_right="8%", pos_top="70%", height="20%"), ) htl = csv_name + ".html" grid_chart.render(htl) wb.open(htl)

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

主函数

def click_success(self): print("数据获取成功!") csv_name1 = 'outside_brent_oil.csv' outside_history_brent_oil_data().to_csv(csv_name1, index=False) show_kline(csv_name1) # html_success() def click_success_3(self): print("数据获取成功!") csv_name2 = 'outside_newyork_oil.csv' outside_history_newyork_oil_data().to_csv(csv_name2, index=False) show_kline(csv_name2) def click_success_4(self): print("数据获取成功!") csv_name3 = 'outside_newyork_gas.csv' outside_history_newyork_natural_gas_data().to_csv(csv_name3, index=False) show_kline(csv_name3) if __name__ == '__main__': app = QApplication(sys.argv) MainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() # 外盘期货 ui.pushButton.clicked.connect(click_success) # 布伦特原油期货分析图 ui.pushButton_3.clicked.connect(click_success_3) # 纽约原油期货分析图 ui.pushButton_4.clicked.connect(click_success_4) # 纽约天然气期货分析图 sys.exit(app.exec_())

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

其中实现跳转代码为

htl = csv_name + ".html" grid_chart.render(htl) wb.open(htl)

1

2

3

4

"

所以不用老去纠结每件事

集中精力完成眼前的事情的过程中

愿望自然就会实现了吧。

"

【纯·干货】你会用到的论文小助手,不定期持续更新中~

HTML Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:什么是GaussDB(for Redis)?
下一篇:云原生下的指标与日志采集
相关文章