PyWebIOUsing the record

The related documents

  • PyWebIO official documentation
  • PyWebIO-Demos
  • This document describes the PyWebIO project address

🎯 Project video 1🎞

🎯 Project video 2🎞

Project introduction

  1. A test program written before using Baidu map APIIdentify picture information
  2. seePyWebIOSo I thought I’d give it a try.
  3. joinPyWebIOThe following functions are available (so that the interface of the application is visualized) :
    • In the browser: the user can select the image to be recognized on the computer
    • Call Baidu picture recognition API
    • In the browser: the image recognition results are intuitively displayed

The end result looks like this:

PyWebIOintroduce

  • For a singlePython application, the use ofPyWebIOCan be very convenient to make the application interface and interaction.I’ve always wanted to use Python to create an application interface.
  • usePyWebIOCan also andDjangoSuch as server-side framework with some output data visualization web page ability.
  • becausePyWebIOUsing browsers as data input and output tools, there should be adaptation issues for different browsers.

andHtmlDifferences between web pages

  • PyWebIOIs theHtmlThe package provided toPythonThe call. The beauty of Python is that it’s simple and convenient, so it’s easy to use Html.
  • withHtml+jsIt can also implement the functions of the application, maybe a little more code. Page frame words can be usedBootStarpAnd so on.
  • So: functions can be implemented, one isA Python program(can also be packaged as an EXE program), the other isThe Html file.

If you deploy your application to a server, you can use the browser to access the web site directly to experience the application’s effects (in this case, the two remain unified).

Use processes in projects

1.pywebiouse

  • usefile_uploadMethod Upload image
    Img = file_upload(" select image ", accept="image/*", placeholder=" view file ", required=True)Copy the code
  • useput_imageMethod will beImage base64Show the data
    img_bs = img['dataurl']
    put_image(img_bs, width='200px')
    Copy the code
  • useuse_scope - clear()Implementation showsloadingAnd hiding effect
    with use_scope('pre-layout'):
        put_loading(shape='grow', color='primary')
        table_res = analysis(img_bs)
        time.sleep(3)
        clear()
    Copy the code
  • useput_tableMethods Display the result of image recognition
    put_table(table_res)
    Copy the code
  • useactionsThe implementation of button blocking procedures, waiting for the user to click the implementation of the corresponding operation. The user clicks “Back” to enter the program entrance again.
    Confirm = actions (', 'return', 'shut down'], help_text = ' ') if confirm = = 'return' : start ()Copy the code
  • Use it directly in the programpywebioFunction, can be directly executed in the codestart()Method can.

2.PythonApplication packagingexeapplication

  • usepip3The installationpyinstallerlibrary
  • usestart_serverMethod to enable a program to startThe web serviceFunction, otherwise openexeUnavailable after application.
    server = start_server(start, port=8089, auto_open_webbrowser=True, debug=True)
    Copy the code
  • usepyinstaller -F baidu.pyCommands can be packagedexeApply, openexeThe program also reported an error.

You need to add pywebio-html to the build/baidu/ directory you just generated. * This Pywebio-html file is the base HTML retrieved in a web request from a web page after launching the native Python application. The functionality of PyWeBio can be understood as being implemented in JS on top of the underlying HTML. * Check out the second section of the PyWeBio library in the Github issue on this topic.

  • Run againpyinstaller -F baidu.pyAfter command, openexeFunction normal after application.

3. Build your ownexeOf the programicoicon

  • Get a favorite image from iconfont or another site
  • inbitbugThe site converts images intoiconIcon, and set to16 * 16The size of the.
  • Will generate the aboveicoPut the icon into the packagepythonProgram directory
  • packagingexeProgram when usingpyinstaller -F -i xxx.ico baidu.pyI can setexeProgram icon

The effect is like this: