Today, I'd like to share a story about interactive development Web app Super simple tool . Can't HTML,CSS,JAVASCRIPT It's okay. .
Interactive Web app Very practical , For example, make a questionnaire page 、 A voting system 、 An information collection form , Upload files and so on , Because web pages are visual , Therefore, it can also be used as a picture interface application without server .
If you have such development needs , The use Python It's so simple .
With the help of PyWebIO(pip install pywebio), You can enter the information of the web page in minutes , You can see the following dynamic diagram :
The page above , Just use this 6 Line code :
from pywebio.input import * input("This is a simple text input") select("This is a drop down menu", ['Option1', 'Option2']) checkbox("Multiple Choices!", options=["a",'b','c','d']) radio("Select any one", options=['1', '2', '3']) textarea('Text Area', rows=3, placeholder='Multiple line text input')
It's also very simple to make the web page output the information you want , The code is as follows , The function has been annotated , It's easy for you to understand :
from pywebio.output import * from pywebio import session # Display plain text on the web page put_text("Hello friend!") # Display table on Web page put_table([ ['Object', 'Unit'], ['A', '55'], ['B', '73'], ]) # The web page shows MarkDown put_markdown('~~PyWebIO~~') # The web page displays a link to download the file put_file('output_file.txt', b'You can put anything here') # Show pictures on Web pages put_image(open('python_logo.png', 'rb').read()) # A pop-up window appears on the web page popup('popup title', 'popup text content') # Keep the reply open , Otherwise, the program exits after the page is displayed session.hold()
The effect of the operation is as follows :
above , A few lines of code implement a with input and output Web application , Isn't that amazing ?
PyWebIO It provides a series of command interactive functions to obtain user input and output on the browser , Turn the browser into a “ Rich text terminal ”, Can be used to build simple Web Application or browser based GUI application .
For example, calculation BMI Exponential script :
from pywebio import start_server from pywebio.input import input, FLOAT from pywebio.output import put_text def bmi(): height = input(" Please enter your height (cm):", type=FLOAT) weight = input(" Please enter your weight (kg):", type=FLOAT) BMI = weight / (height / 100) ** 2 top_status = [(14.9, ' Extremely thin '), (18.4, ' Thin '), (22.9, ' normal '), (27.5, ' overweight '), (40.0, ' obesity '), (float('inf'), ' Very fat ')] for top, status in top_status: if BMI <= top: put_text(' Yours BMI value : %.1f, Physical condition :%s' % (BMI, status)) break if __name__ == '__main__': start_server(bmi, port=80)
effect :
More examples :
PyWebIO It also supports data visualization using third-party libraries
Data visualization rendering :
PyWebIO It can also be easily integrated into existing Web service , So you don't have to write HTML and JS Code , You can build applications with good usability . At present, we support Flask、Django、Tornado、aiohttp、FastAPI Framework for the integration .
For example, with Tornado Application Integration :
import tornado.ioloop import tornado.web from pywebio.platform.tornado import webio_handler class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") if __name__ == "__main__": application = tornado.web.Application([ (r"/", MainHandler), (r"/bmi", webio_handler(bmi)), # bmi Is calculated above BMI Exponential function ]) application.listen(port=80, address='localhost') tornado.ioloop.IOLoop.current().start()
And Django Integrate :
# urls.py from django.urls import path from pywebio.platform.django import webio_view # `task_func` is PyWebIO task function webio_view_func = webio_view(task_func) urlpatterns = [ path(r"tool", webio_view_func), ]
PyWebIO Get input using synchronization rather than callback based methods , Coding logic is more natural , Non declarative layout , The layout is simple and efficient , The code is less intrusive , The old script code can be transformed into Web service , It can also be integrated into the existing mainstream Web frame . Both thread based execution model and co process based execution model are supported , Support data visualization in combination with third-party libraries
In a word ,PyWebIO Let interactive Web Development becomes easier , It is especially suitable for developing simple GUI Program and data visualization .