Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the data Inquirer of Web Page by Python PyWebIO

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Python PyWebIO how to implement the web version of the data query", the explanation in the article is simple and clear, easy to learn and understand, please follow the small series of ideas slowly in-depth, together to study and learn "Python PyWebIO how to implement the web version of the data query"!

The interface has always been Python's pain! Making desktop interfaces with Python is a painful (and ugly) process. However, Python already has several web-based front-end libraries that share the same basic mechanics, and if you don't have a lot of interface manipulation requirements, these libraries are good for you.

This series is based on pywebio's series of practical applications, let's learn how to use this library from actual combat!

The final effect of this section:

Select Excel file

Output the data of the first worksheet of the file (DataFrame)

Installation Library:

pip install -U pywebio

output text

First output a paragraph of content to try the effect:

import pywebioimport pywebio.output as outputdef main(): output.put_html ('table display') if __name__=='__main__': pywebio.start_server(main, port=8080, debug=True, cdn=False,auto_open_webbrowser=True)

Lines 1, 2: Import what you need, where pywebio.output is used to output content-dependent operations

Line 4: Define a function with an arbitrary name

Line 5: put_html This function is the operation of the output module imported in line 2 to output html content.

Line 9: Start the service. This is mostly boilerplate code (i.e., the same code every time). Especially important is the first parameter, which is the name of the function we defined (note that it is not executed, just passed in)

Execute this script, and if nothing else, your browser will launch a page at http://localhost:8080:

If you're not familiar with html tags, you may not even be able to do basic typography.

Pywebio also supports markdown:

def main(): output.put_markdown ('#table displayer') output.put_markdown ('function as follows: ') output.put_markdown("""-Select file-Automatically load part of output table """)

input operation

Only the output operation is not enough to meet the demand, and there are many input operations on the interface.

Selecting a file is an input operation (the user enters content into the interface):

import pywebioimport pywebio.output as outputimport pywebio.input as inputdef main(): output.put_markdown ('#table displayer') output.put_markdown ('function as follows: ') output.put_markdown("""-Select file-Automatically load part of output table """) file = input.file_upload ('select an excel file','. xlsx')

Line 3: Import input operation module

Line 13: File_upload is used to let the user select the file to upload. The first parameter is the prompt content, and the second parameter is the qualified file suffix

The file selected by the user will be assigned to the variable file

See the effect:

Click the Browse button on the right to select Excel files from the pop-up window.

After clicking the submit button, the previous code will continue to execute.

With the file content, we read it with pandas:

import pywebioimport pywebio.output as outputimport pywebio.input as inputimport pandas as pddef main(): output.put_markdown ('#table displayer') output.put_markdown ('function as follows: ') output.put_markdown("""-Select file-Automatically load part of output table """) file = input.file_upload ('select an excel file','. xlsx') df = pd.read_excel(file['content']) output.put_html(df.head(10).to_html())

Line 15: Get the file contents via file ['content '].

Line 16: df.to html() will generate the complete table html, and then put html output

See the effect:

focus

The final code is as follows:

import pywebioimport pywebio.output as outputimport pywebio.input as inputimport pandas as pddef main(): output.put_markdown ('#table displayer') output.put_markdown ('function as follows: ') output.put_markdown("""-Select file-Automatically load part of output table """) file = input.file_upload ('select an excel file','. xlsx') df = pd.read_excel(file['content']) output.put_html(df.head(10).to_html())

All input operation codes will wait for the interface operation to be completed before proceeding. For example, line 14 file_upload will remain stuck until the "Submit" button is clicked on the interface.

Following the above procedure, this function will complete. If you want to do it again, just refresh your browser page.

It's just not fun loading data, you know? The next section will further improve the ability to query data after loading it

Thank you for reading, the above is the content of "Python PyWebIO how to implement the web version of the data query", after the study of this article, I believe that we have a deeper understanding of Python PyWebIO how to implement the web version of the data query, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report