Location>code7788 >text

Research on building many different system terminal interfaces based on Python backend

Popularity:478 ℃/2024-10-10 11:21:18

In our general development of the system, often according to the actual need to make a variety of different system terminal interface, such as BS-based, CS, APP, applets, etc., generally based on a unified access to the Web API back-end, this system to explore the Python back-end based on the construction of a variety of different system terminal interface research, to introduce some of the different back-end based on the development of the Python backend terminal interface.

1. Python system terminal interface

I wrote about it in the accompanying article.Python development framework based on SqlAlchemy+Pydantic+FastApiThe book describes the backend of FastAPI based on Python to build a complete Web API interface to adapt to the access of multiple terminals.

Based on Python's FastAPI back-end , follow the Restful Web API standards , by way of generalization and base class encapsulation , the abstract encapsulation of most commonly used CRUD interfaces , the actual business according to our actual needs to extend the interface can be .

For example, when the base class controller is defined, it is sufficient to initialize the routing according to the provided Web API interface.

Base class controllers mainly call CRUD classes for data processing, so each business has its own CRUD class for data manipulation encapsulation, and they are also inherited by the base class.

We have implemented in the base class Crud class of conventional data processing logic, including conventional paging queries, updates, deletion, find, insert, and whether there is a record, sorting processing and a series of general operations, if we need to add additional data processing operations, then in the subclasses can be added to the function interface.

For data transfer and processing, we use pydantic objects and SqlAlchemy's model objects, which are sufficient for processing.

After starting the project, you can see the detailed Swagger documentation on the WebAPI homepage, which is very convenient for reference.

With the unified Web API backend, we can extend the realization of our own system business endpoints as needed.

We have written a number of complete Winform interfaces and BS-side interfaces for business processing, and we integrate Python's Web API interfaces as needed.

We modify the API path that specifies the configuration of the Winform front-end, so that it points to the Python Web API interface, you can dock the Winform front-end successfully.

And for the BS front-end interface of Vue3+ElementPlus, since the front-end and back-end are in a strict separation mode, just handle it the same way.

 

2、Python based terminal interface research

The above describes the Winform interface development based on .net, and the BS end interface development based on Vue3+ElementPlus+Typescript, which is based on the Python backend for the terminal development of other languages.

So can we also use native Python to develop the interface, the answer is of course yes.

The use of native Python development similar to the desktop side of Winform , but also a set of code compiled to achieve cross-platform development , able to run on Windows systems , MacOS systems , and other Linux systems .

1) wxpython+wxFormBuilder combination

Such as based on VSCode + wxpython + wxFormBuilder combination to achieve the desktop side of the development, of course, some people can also use PyQT (PySide) to develop the desktop side.

wxpython: /

wxFormBuilder: /wxFormBuilder/wxFormBuilder

Let's take a look at the simple wxpython code, which is essentially

# First things, first. Import the wxPython package.
import wx

# Next, create an application object.
app = ()

# Then a frame.
frm = (None, title="Hello World")

# Show it.
()

# Start the event loop.
()

In the Mac and Windows interfaces below.

/images/

 /images/

wxpython mainly through the coding method , the use of various layout controls , similar to the Winform way , but we can also use wxFormBuilder to generate the interface code , and then inherit it to use .

For example, we can use wxFormBuilder to generate the interface code for export to VScode can be used.

We can also view the generated Python code in real time in the form interface

We can use it to write some complex interfaces, such as paging based on data tables.

A general dialog interface that can be used to edit data, as shown below Layout processing implemented using Layout and can be dragged to zoom in and out of the interface

The Window interface is not very pretty, but it is possible to achieve the same effect on MacOS with one set of code, so we can make a management system, compile it and use it on different systems.

Note: MacOS use needs to be compiled as an App by PyInstaller on VSCode on Mac, while on Windows the same can be compiled as an exe file using PyInstaller on VSCode.

 

2) Use nicegui to build a BS or standalone program to use it

In addition to the combination of wxpython and wxFormBuilder introduced above to achieve cross-platform terminal development, you can also use PyQT and other series of components to do, but here is no longer in-depth introduction to these same types of components.

Let's introduce a more emerging one of the interface master key nicegui to develop:

Nicegui's official website:/documentation

Github address:/zauberzeug/nicegui

It is a program that can create a BS front-end that runs server-side based, or it can be a stand-alone program, similar to Electron (/)'s stand-alone program. Depending on how it is compiled, different files are generated.

It realizes the generation of the interface through a simple coding method, integrates the use of many common controls, and provides many cases for reference.

For example, I compiled it as a standalone program, similar to Electron's desktop program, to run separately.

(native=True)

This one allows you to use styles to achieve a rich effect similar to the regular BS interface, which is indeed a bit more beautiful than the previous Wxpython interface.

If the program runs on a browser, then it's a proper BS front end.

In this way, no matter it is required that kind of interface way, can use a set of code to unify the implementation, and nicegui also provides a lot of case references.

However, nicegui also has some shortcomings, is the use of the time, found that it for some of the effects of the interface, or to provide not enough case code, many times need to be combined with theTailwind CSS to achieve richer interface effects.