50 lines of Python code to create a big data screen!

王林
Release: 2023-04-13 10:13:05
forward
1886 people have browsed it

50 lines of Python code to create a big data screen!

Introduction to PywebIO

The PywebIO module in Python can help developers quickly build web applications or browser-based applications without HTML and JavaScript. For GUI applications, PywebIO can also be used in conjunction with some commonly used visualization modules to create a large visual screen.

Let’s first install the modules we need to use.

pip install pywebio
pip install cutecharts
Copy after login

The cutecharts module mentioned above is a hand-drawn style visualization artifact in Python. I believe everyone is familiar with it. Let’s take a look at the effect of combining it with the PywebIO module to draw charts. The code 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)
Copy after login

output

50 lines of Python code to create a big data screen!

The logic of the above code is not difficult to understand. First instantiate a histogram Bar() object, and then fill in the X axis The corresponding label and the corresponding Y-axis value, and finally call the put_html() method in the PywebIO module, we will see a URL.

50 lines of Python code to create a big data screen!

#Enter the URL in the browser to see the chart we drew. Of course, there is a Page() method in the cutecharts module to connect various charts to create 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)
Copy after login

output

50 lines of Python code to create a big data screen!

The combination of PywebIO and Pyecharts

When the PywebIO module meets the Pyecharts module, the logic of the code is basically the same as that of cutecharts. First, a chart object is instantiated, and then after adding the data and setting up the chart After styling, the put_html() method is finally called to render the final result in the browser.

# `chart` 是你的图表的实例
pywebio.output.put_html(chart.render_notebook())
Copy after login

In this case, we call the combination component in Pyecharts to present the completed charts 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)
Copy after login

output

50 lines of Python code to create a big data screen!

The combination of PywebIO and Bokeh

The combination of PywebIO and Bokeh will be slightly different from the above in terms of code syntax. 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)

Copy after login

For example, we To 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)

Copy after login

output

50 lines of Python code to create a big data screen!

##Browser-based GUI application

In addition to the Pywebio module In addition to combining with commonly used visualization modules for drawing various charts, we can also use it to build a browsing-based graphical interface. Let's first make the simplest application. 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"]],
]
)

Copy after login
output

50 lines of Python code to create a big data screen!

The explanations of some of the function methods are as follows:

    input(): input of text content
  • radio(): Represents the single-select box
  • checkbox(): Represents the multi-select box
  • input_group(): Represents the input group
  • put_table(): Represents the Output group
  • put_text(): represents the output text

The above is the detailed content of 50 lines of Python code to create a big data screen!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template