tags: JavaScript Python
For more information, please visit me.personal blog。
Nowadays, to say that the programming language of the comparison is JavaScript and Python. These two languages can be independently written, backend servers, mobile apps, computer clients, etc., omnipos. However, different programming languages have different focuses. For example, JavaScript write web pages should be worn, Python handles big data letter hand. So, can you take advantage of the two, build a cross-platform client framework? This has the protagonist today:vue-pywebview-pyinstaller。
vue-pywebview-pyinstaller It is an application based on Vue3, PywebView and Pyinstaller framework, build MacOS and Windows platform clients. The view layer of this application uses HTML + JS + CSS, and the business layer is local Python. Considering that some biological calculation scene data is large, data is private, so the data is uploaded to the server calculation, and it is not necessarily the optimal solution, and the choice of local Python is also a good choice. However, if you need to call the remote API, this application is also supported.
Familiar with Vue3 and Python programmers.
Notice: Under the Windows system, because PyWebView3 relying on the PythonNet package only supports Python3.5-3.8 version, it is recommended that Win users use Python 3.8 version.
Use git (Git installation tutorialDownload the app, as shown below:
git clone https://github.com/pangao1990/vue-pywebview-pyinstaller.git
Or, directly in mygithub download.
#
cd vue-pywebview-pyinstaller
Enter the project, the list of projects is as follows:

After the download is complete, run the initialization command, the program will automatically download the required dependency software installation corresponding to the operation platform, as shown below:
# Initialization
npm run init
If there is no error message, the initialization is complete, as shown below:


There is a Node_Modules folder in the project, used to store NPM download packages; there is a Pyenv folder to store the Python virtual environment.
Enter the following command to start the application:
npm run start
The terminal is shown as follows:

At the same time, a client program will start, as follows:

The overall effect is as follows (GIF picture load may be slow):

This application is based onpywebview Build a client. The principle of the PywebView architecture construction client is to use the browser engine driver comes with the local computer to simulate the generated client. Essentially is also a web page, or a browser, but there is no difference between the sensory and the local client.
Then, based on the success or quality of the PywebView architecture, it is closely related to the local computer's browser engine.
On the Windows system, it is generally divided into two types of client engines: normal mode and compatibility mode.
In normal mode, in order to retrieve in the client engine of Edgechromium, Edgehtml, MSHTML. If the local computer EDGE browser supports these engines, the client can start normally. Otherwise, please install the correspondingEdgeWebView2Runtime The browser engine.
If the local computer Edge browser does not support these engines, don't want to downloadEdgeWebView2Runtime Then you can use a compatibility mode. The principle of compatibility model is to useCEFPythonEmbed a WEB browser control of Chromium. That is, as long as the local computer has installed Google browser V66 and its above, the client can be started normally. Disadvantages is that the generated installation package volume will increase approximately 60m.
There is not so much version of the MacOS system browser engine. Due to the closure of the MacOS system, there is only one webkit engine available in the MacOS system.
However, there is another problem in the MacOS system. That is the M1 chip of Apple's independently developed. Since the Windows system is visible everywhere, I can find a lot of computer tests. I only have a M1 chip and an X86_64 chip and can't do more tests. In short, the client in which I have built in the two Apple computers cannot cross the use, and the reason is unknown.
Building a client's main program is main.py and maincef.py under the Pyapp folder, as shown below:

The contents of the maincef.py script are almost exactly the same, that is, a CEF mode is added to the compatibility mode of the Windows system.

Main.py mainly relying on WebView.create_Window and WebView.Start two APIs to build clients. Other some APIs, I will also introduce in the following tutorial. Or you can view directlyPywebView official website Learn more.
webview.create_window(title, url='', html='', js_api=None, width=800, height=600, \
x=None, y=None, resizable=True, fullscreen=False, \
min_size=(200, 100), hidden=False, frameless=False, \
minimized=False, on_top=False, confirm_close=False, \
background_color='#FFF')
Create a new PyWebView window and return it. The window does not display before starting the GUI loop.
webview.start(func=None, args=None, localization={}, gui=None, debug=False, http_server=False)
Start the GUI loop and display the previously created window. This function must be called from the main thread.
Window.evaluate_JS (CODE, CALLBACK = NONE) Allows you to perform any JavaScript code using the last value returned by the synchronous returns. If the callback function is provided, the promise is parsed, and the callback function is called, and the result is the parameter. The JavaScript type is converted to a Python type, such as the JS object to the Python dictionary, array to the list, is not defined as None. Due to implementation, string "null" will be calculated as NONE. In addition, the value returned by EVALUATE_JS is limited to 900 characters.
Calling the Python function from JavaScript can be done by two different ways.
PywebView suggests MacOSpy2app Pack, Windowspyinstaller Pack. But I found that Pyinstaller can also be able to pack MacOS applications, although the transplant is a bit problem.
I didn't introduce Pyinstaller's packaging method, and I will have a detailed package introduction of this framework. Here I will pack the package method in the application, just pack it by command.
# , with console, easy to output log information
npm run pre
# , with console, compatibility mode, only WIN system
npm run pre:cef
# , with console, generate folders, only WIN system
npm run pre:folder
# , with console, generate folders, compatible mode, only WIN system
npm run pre:folder:cef
# Officially packaged
npm run build
# Officially packaged, compatible mode, only WIN system
npm run build:cef
# Officially packaged, generate folders, only WIN system
npm run build:folder
# Officially packaged, generate folders, compatible mode, only WIN system
npm run build:folder:cef
For more programming teaching, please pay attention to the public number:Pan Gao accompany you to program

https://electronjs.org/ - official website https://github.com/electron/electron-api-demos/releases - download demo Download and install: Plug-in installation package / installation and p...
Electron develops desktop applications Use Electron to develop js version of desktop applications. Electron's official website:http://www.electronjs.org/ We can see a lot of information on the officia...
After we have created the project, what we may start to do is to quickly develop the project; but in order to avoid accidents during the development process (such as: computer failure, or work at home...
1. Project construction Use vue-cli to build the project, check the official documentation for the build method vue-cli documentation 2. Install the plugin Method one: (visual installation) vue uiOpen...
Write catalog title here 1. Create a project 1.1 Install Nodejs 1.2 Use cnpm to speed up downloads 1.3 Why not use electron-vue to build it 1.4 Install/upgrade vue-cli3 1.5 Create a vue project 1.6 Au...
Electron+Vue develops cross-platform applications Official document Come to a Hello world Install Electron+Vue Install vue-cli and scaffolding boilerplate code Official document https://simulatedgreg....
table of Contents 1. Create a project 1.1 Install Nodejs 1.2 Use cnpm to speed up downloads 1.3 Why not use electron-vue to build it 1.4 Install/upgrade vue-cli3 1.5 Create a vue project 1.6 Automatic...
Introduction From the first quarter, each period will be updated in the future (I have issued original teaching in social media, the creation is not easy, and hope to support) Electron takes you to un...
table of Contents Basic comprehensive introduction: Environmental construction: Install Electron The Electron application is divided into three basic modules: the main process, inter-process com...
foreword Because I changed my computer to a Mac and connected my mobile phone to the computer, I couldn’t operate it. I found some software from the Internet and I don’t know why I couldn&...