Today I will share with you a tool for making a large data screen. It is very easy to use. Python
A complete large data screen can be made with about 100 lines of code, and the logic of the code is very easy to understand.
PywebIO
introduce
Python
The PywebIO
modules in it can help developers to quickly build applications or browser-based applications even if they do not have HTML
and can also be combined with some commonly used visualization modules to make a large visual screen.JavaScript
Web
GUI
PywebIO
Let's first install the modules we need to use
pip install pywebio
pip install cutecharts
cutecharts
The module mentioned above is Python
a hand-painted style visual artifact. I believe you are not unfamiliar with it. Let's take a look at PywebIO
the effect of drawing a chart in combination with the module. The code is as follows
from cutecharts.charts import Bar
from cutecharts.faker import Faker
from pywebio import start_server
from pywebio.output import put_html
def bar_base():
chart = Bar("Bar-基本示例", width="100%")
chart.set_options(labels=Faker.choose(), x_label="I'm xlabel", y_label="I'm ylabel")
chart.add_series("series-A", Faker.values())
put_html(chart.render_notebook())
if __name__ == '__main__':
start_server(bar_base, debug=True, port=8080)
output
The logic of the above code is not difficult to understand. First instantiate a histogram Bar()
object, then fill X
in the label corresponding to the Y
axis and the value of the corresponding axis, and finally call PywebIO
the method in the module put_html()
, we will see aURL
Enter this in the browser URL
to see the chart we drew. Of course cutecharts
, there is a method in the module Page()
to connect all the charts to make a large visual screen. The code is as follows
def bar_base():
chart = Bar("Bar-基本示例", width="100%")
chart.set_options(labels=Faker.choose(), x_label="I'm xlabel", y_label="I'm ylabel")
chart.add_series("series-A", Faker.values())
return chart
def pie_base() -> Pie:
chart = Pie("标题", width="100%")
........
return chart
def radar_base() -> Radar:
chart = Radar("标题", width="100%")
......
return chart
def line_base() -> Line:
chart = Line("标题", width="100%")
......
return chart
def main():
page = Page()
page.add(pie_base(), pie_base(), radar_base(), line_base(), bar_base())
put_html(page.render_notebook())
if __name__ == '__main__':
start_server(main, debug=True, port=8080)
output
PywebIO
Pyecharts
combination of and
When a PywebIO
module encounters Pyecharts
a module, the logic of the code is basically cutecharts
the same, first instantiate a chart object, then after adding data and setting the style of the chart, and finally calling put_html()
the method to render the final result in the browser
# `chart` 是你的图表的实例
pywebio.output.put_html(chart.render_notebook())
In this case, we call Pyecharts
the combined components to present the drawn chart respectively. The code is as follows
def bar_plots():
bar = (
Bar()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Grid-Bar"))
)
return bar
def line_plots():
line = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Grid-Line", pos_top="48%"),
legend_opts=opts.LegendOpts(pos_top="48%"),
)
)
return line
def main():
c = (
Grid()
.add(bar_plots(), grid_opts=opts.GridOpts(pos_bottom="60%"))
.add(line_plots(), grid_opts=opts.GridOpts(pos_top="60%"))
)
c.width = "100%"
put_html(c.render_notebook())
if __name__ == '__main__':
start_server(main, debug=True, port=8080)
output
PywebIO
Bokeh
combination of and
PywebIO
The combination of and Bokeh
will be slightly different from the above in terms of code syntax, and the specific differences are as follows
from bokeh.io import output_notebook
from bokeh.io import show
output_notebook(notebook_type='pywebio')
fig = figure(...)
...
show(fig)
For example, let's draw a simple histogram, the code is as follows
def bar_plots():
output_notebook(notebook_type='pywebio')
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]
p = figure(x_range=fruits, plot_height=350, title="Fruit Counts",
toolbar_location=None, tools="")
p.vbar(x=fruits, top=counts, width=0.9)
p.xgrid.grid_line_color = None
p.y_range.start = 0
show(p)
if __name__ == "__main__":
start_server(bar_plots, debug=True, port=8080)
output
browser-based GUI
application
In addition to combining Pywebio
modules with common visualization modules for drawing various charts, we can also use it to build a graphical interface based on browsing. Let's make the simplest application first. The code is as follows
from pywebio.input import *
from pywebio.output import *
data = input_group(
"用户数据",
[
input("请问您的名字是: ", name="name", type=TEXT),
input("输入您的年龄", name="age", type=NUMBER),
radio(
"哪个洲的",
name="continent",
options=[
"非洲",
"亚洲",
"澳大利亚",
"欧洲",
"北美洲",
"南美洲",
],
),
checkbox(
"用户隐私条例", name="agreement", options=["同意"]
),
],
)
put_text("表格输出:")
put_table(
[
["名字", data["name"]],
["年龄", data["age"]],
["位置", data["continent"]],
["条例", data["agreement"]],
]
)
output
Some of the function methods are explained as follows:
-
input()
: Input of text content -
radio()
: represents a radio button -
checkbox()
: Represents a multi-select box -
input_group()
: represents the input group -
put_table()
: represents the output group -
put_text()
: represents the output text
Here I would like to recommend the Python learning Q group I built by myself: 831804576. Everyone in the group is learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and shares dry goods from time to time ( Only related to Python software development),
including a copy of the latest Python advanced materials and zero-based teaching in 2021 that I have compiled by myself. Welcome to the advanced middle and small partners who are interested in Python!