chart / app.py
changxin's picture
Update app.py
322b4e5
import gradio as gr
import pygal
import requests
import pandas as pd
import pandas_bokeh
import matplotlib.pyplot as plt,mpld3
import seaborn as sns
import bokeh.plotting as bk
from bokeh.embed import json_item
from highcharts import Highchart
import pyecharts
from pyecharts.charts import Bar
import cufflinks as cf
import plotly.offline
cf.go_offline()
cf.set_config_file(offline=True, world_readable=True)
#from pyg2plot import Plot
#plt.rcParams['font.sans-serif']=['SimHei']
#plt.rcParams['axes.unicode_minus']=False
def pd_bar():
pd.options.plotting.backend='matplotlib'
plt.close('all')
df = pd.DataFrame({'课程':['语文', '数学', '英语'], '成绩':[66, 98, 78]})
ax = df.plot(x='课程',y='成绩',kind='bar',title='chinese word error!', color=["red","green","purple"])
ax.table(cellText=df.values,colLabels=df.columns,loc='bottom',colLoc='center')
return ax.get_figure() #plt
def plt_line():
plt.close('all')
df = pd.DataFrame({'课程':['yw', 'sx', 'yy'], '成绩':[66, 98, 100]})
plt.plot(df['成绩'],color='green',linestyle='dashed',marker='o',markerfacecolor='blue',markersize=20)
return plt.gcf()
def pd_plotly():
pd.options.plotting.backend='plotly'
df = pd.DataFrame({'课程':['语文', '数学', '英语'], '成绩':[66, 98, 78]})
ax = df.plot.bar(x='课程',y='成绩')
return ax #plt
def pd_bokeh():
pd.set_option('plotting.backend','pandas_bokeh')
pandas_bokeh.output_file("test.html")
df=pd.DataFrame({'班级':['天','地','玄','黄'],'姓名':['诸葛清明','公孙轩辕','李耳','石破天'],'成绩':[66,78,99,85]})
tt=df.plot_bokeh.bar(x='姓名',y='成绩',ylabel="群员吹水战绩",title="吹牛逼你们是认真的",alpha=0.6,figsize=(600,300),show_figure=False,legend="bottom_right")
pandas_bokeh.save(tt)
p = bk.figure(plot_width=400,plot_height=400)
p.patch([1,2,3,4,5],[6,7,8,7,3],alpha=0.5,line_width=2)
item_text = json_item(p, "plotDiv")
return 'test.html',item_text
def pyecharts1():
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.render('test.html')
with open('test.html',"r",encoding="utf-8") as aa:
bb=aa.read()
return 'test.html',bb
def cff():
df=pd.DataFrame([[0.2,0.4,0.6],[0.4,0.3,0.4],[0.4,0.5,0.2],[0.5,0.4,0.4]],columns=['a','b','c'])
cfft=df.iplot(kind='bubble',x='a',y='b',size='c')
return cfft
def seab():
df = pd.DataFrame({'a': range(10), 'b': [1,3,1,4,5,2,0,5,8,5]})
g=sns.relplot(x="a", y="b", kind="line", data=df)
return plt
def pyg2():
return ''
#def pyg2plot_chart():
# bar = Plot("Column")
# bar.set_options({"height": 600,"width":800,"data": [{ "年": "2001", "销量": 3 },{ "年": "2002", "销量": 4 },{ "年": "2003", "销量": 5.5 },{"年": "2004", "销量": 7 },{ "年": "2005", "销量": 8.9 },{ "年": "2006", "销量": 10}],"xField": "年","yField": "销量","label": {}})
# return bar.render_html()
def pyhighchats():
H = Highchart()
data = list(range(1,20))
H.add_data_set(data,'line')
H.save_file('test.html')
return 'test.html'
def d3_js():
fig=plt.figure()
obj,=plt.plot([3,1,4,1,5])
rr=mpld3.fig_to_html(fig)
return rr
def pygalc():
aa=pygal.Bar()(1,3,3,7)(1,6,6,4)
bb=aa.render(is_unicode=True)
cc=aa.render_data_uri()
aa.render_to_file("test.svg")
return 'test.svg',bb,cc,bb
demo=gr.Blocks()
with demo:
gr.Markdown("# <center>图表渲染测试")
with gr.Tabs():
with gr.TabItem("pd_bar"):
pd_button=gr.Button("pd_bar>>")
pd_output=gr.Plot(label="pd_chart1")
pd_button.click(pd_bar, inputs=[], outputs=pd_output,api_name='pd_bar')
with gr.TabItem("plt_line"):
plt_button=gr.Button("plt_line>>")
plt_output=gr.Plot(label="plt_chart")
plt_button.click(plt_line, inputs=[], outputs=plt_output,api_name='plt_line')
with gr.TabItem("pd_plotly"):
plotly_button=gr.Button("pd_plotly>>")
plotly_output=gr.Plot(label="plotly_chart")
plotly_button.click(pd_plotly, inputs=[], outputs=plotly_output,api_name='pd_plotly')
with gr.TabItem("pd_bokeh"):
bokeh_button=gr.Button("pd_bokeh>>")
bokeh_output=[gr.File(label="bokeh_chart"),gr.Plot(label="bokeh_chart2")]
bokeh_button.click(pd_bokeh, inputs=[], outputs=bokeh_output,api_name='pd_bokeh')
with gr.TabItem("pyecharts"):
ehc_button=gr.Button("pyecharts>>")
ehc_output=[gr.File(label="pyecharts_chart"),gr.Textbox(label="pyecharts_chart2",lines=18)]
ehc_button.click(pyecharts1, inputs=[], outputs=ehc_output,api_name='pyecharts')
with gr.TabItem("cufflinks"):
cff_button=gr.Button("cufflinks>>")
cff_output=gr.Plot(label="cufflinks_chart")
cff_button.click(cff, inputs=[], outputs=cff_output,api_name='cuffl')
with gr.TabItem("seaborn"):
sb_button=gr.Button("seaborn>>")
sb_output=gr.Plot(label="seaborn_chart")
sb_button.click(seab, inputs=[], outputs=sb_output,api_name='seaborn')
with gr.TabItem("pyg2plot"):
g2plot_button=gr.Button("pyg2plot>>")
g2plot_output=gr.Plot(label="pyg2plot_chart")
g2plot_button.click(pyg2, inputs=[], outputs=g2plot_output,api_name='pyg2plot')
# with gr.TabItem("pyg2plot"):
# pyg2_button=gr.Button("pyg2plot>>")
# pyg2_output=gr.Plot(label="pyg2plot_chart")
# pyg2_button.click(pyg2plot_chart, inputs=[], outputs=pyg2_output)
with gr.TabItem("pyhighcharts"):
pyhc_button=gr.Button("pyhc>>")
pyhc_output=gr.File(label="pyhc_chart")
pyhc_button.click(pyhighchats, inputs=[], outputs=pyhc_output,api_name='pyhcharts')
with gr.TabItem("d3JS"):
d3_button=gr.Button("D3JS>>")
d3_output=gr.Textbox(label="D3_chart1",lines=18)
d3_button.click(d3_js, inputs=[], outputs=d3_output,api_name='d3_js')
with gr.TabItem("pygal"):
pyga_button=gr.Button("pygal>>")
pyga_output=[gr.File(label="pygal_chart"),gr.Textbox(label="pygal_chart1",lines=10),gr.Textbox(label="pygal_chart2",lines=3),gr.HTML(label="html渲染")]
pyga_button.click(pygalc, inputs=[], outputs=pyga_output,api_name='pygal')
with gr.TabItem("echarts"):
with gr.Accordion("可伸缩扩展布局容器",open=False):
gr.Markdown("测试伸缩布局")
# gr.Carousel()新组件待测试,,,
htmls='''
<html>
<head><style type="text/css">body {margin: 0;}#main2 {height: 100%;}</style></head>
<body><div id="main2"></div>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.0.2/dist/echarts.min.js"></script>
<script>var myChart = echarts.init(document.getElementById("main2"));
option = {tooltip:{formatter:"{a} <br/>{b} : {c}%"},series:[{name:'业务指标',type:'gauge',detail:{formatter:'{value}%'},axisLine:{lineStyle:{width:20}},splitLine:{length:20},data:[{value:50,name:'完成率'}]}]};
myChart.setOption(option);
</script></body>
</html>
'''
gr.HTML(htmls)
gr.HTML("https://hf.space/static/changxin/highcharts/index.html")
demo.launch()