DelphiFMXPythonPython GUI

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI

APIs, or Application Programming Interfaces, can be an extremely useful way of making your software accessible to users, potentially across the globe. Good programming language frameworks make it simple to create high-quality products quickly. They can even make the development process more enjoyable. FastAPI is a new Python web framework that checks all the boxes for being an excellent API development framework and is powerful and efficient. In addition, it integrates well with many Python GUI applications.

In this article, we will look at some interesting features of FastAPI, focusing on async methods and how to use them to create web APIs quickly. By the end, you’ll be able to start creating production-ready web APIs. We will then learn about creating your own Python GUI using DelphiFMX and PyScripter which I think is the best Python IDE. Finally, we will integrate the web API into the GUI application.

What is the significance of asynchronous programming?

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a laptop showing the FastAPI download page

Asynchronous programming is a multi-tasking paradigm where a unit of work can run independently of the primary application thread. When the work is finished, it notifies the main thread of the worker thread’s success or failure.

This programming method has many key advantages; for instance, it is faster and more responsive than its synchronous counterpart. A program with asynchronous methods can become faster and present information to the user as it is processed, never allowing the project to become unresponsive and thus providing a better user experience.

This kind of programming even allows improved testing and error handling. If an operation in a different thread fails for whatever reason, you can handle the error or anomaly without restarting the entire project.

Furthermore, async ensures increased scalability since you can easily add new features or increase processing power to handle more requests by offloading operations to different threads.

Unfortunately, it also comes with some disadvantages, such as code complexity where code can become significantly more complex and difficult to debug, and thread overload which is where you overdo the amount of work the thread is doing which then impacts the synchronicity of your project so that becomes a hindrance rather than a help.

So, using asynchronous programming over synchronous programming isn’t a matter of what is better; it is a matter of when to use what.

You’ll generally want to use synchronous programming if your project focuses on a single task or if the tasks are highly time or response dependent and cannot run in parallel, such as web pages or video rendering. When you have a lot of tasks that don’t depend on each other or tasks that take a long time and can be run in the background, such as database manipulation, you’ll want to use asynchronous programming. Thus, any management system would be a good use case for async programming.

What is Python AsyncIO?

AsyncIO is a co-routine-based concurrency model in Python that provides elegant constructs for writing concurrent Python code without using threads.

This standard library package enables developers to create concurrent programs using async/await syntax. It enables developers to create unique functions known as co-routines for asynchronous programming by using a simple async keyword and is thus regarded as the ideal fit for IO-bound and high-level structured network code in Python.

Is it possible to build an asynchronous Python service with FastAPI?

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a laptop with a person editing a Python script

Yes, it is! Let us move on and start building an asynchronous employee management service in Python. But before we start with this application, there are some prerequisites that you need to install.

What are the prerequisites for creating an asynchronous service with Python?

First of all, you will need to install FastAPI and Uvicorn packages. You can easily install them using pip by running:

FastAPI is a fairly new python web framework with built-in support for async API endpoints. The library will be used to construct a custom API for our employee management service. Meanwhile, Uvicorn is our chosen Asynchronous Server Gateway Interface (ASGI) server. This will help us run our app.

Next, you will need to install SQLAlchemy and aiosqlite using:

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper (ORM) that gives application developers the full power and flexibility of SQL. We will use this to create our database that will store all employee information while we use aiosqlite to interact with our database.

We can start building our service once we’ve installed our dependencies.

You can grab the code for this tutorial from our git repository at: github.com/Embarcadero/PythonBlogExamples/tree/main/Async_FastAPI_GUI_client

How to Use SQLAlchemy and AsyncIO to Create a Database?

Let’s begin by creating a database to store our Employee data.

First, we will configure our database using a simple Python script. To do this, create a file named employee.py and first import the necessary packages:

Next, we will create a local instance of SQLLite (a database named employees.db):

We will use aiosqlite to communicate with the database as it supports async queries.

So, we create a database engine using the create_async_engine function.

Then sessionmaker creates a session with two unique flags, expire_on_comit, and class_. We set expire_on_comit to False, ensuring that our database entries and fields are always available even after a commit is made on our server session. Setting class_ to AsyncSession lets our database know this is a new async session:

Now, we are ready to configure our database.

Since we’re building an employee management service, our main database would contain some identifying attributes like id and firstName along with further details, such as joinYear, which gives the year when the employee joined the organization. We can define our database by using the Base we declared previously:

Note that using the __tablename__ attribute above, we initialize the name of the database. We then assign each column a respective Column object, assign their data types, and declare whether they can be null using nullable, and specify whether they can be used as a primary_key for the database.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a screen showing the required FastAPI packages being imported

Here is what our final employee.py file looks like:

To create a new entity, we will use a Data Access Layer (DAL), which will be responsible for all the SQL functions for our database. We do this by creating a new file, employee_dal.py, and adding some package imports:

Next, we define a new class named EmployeeDAL that will have four methods:

The create_employee() function receives details regarding an employee and creates a new Employee object. It then uses the object to save a new record to the database. A get_employees() function returns all the employees in the database in ascending order based on the employee id. The update_employee() function receives an employee id and optional new fields for the employee to update in the database. Lastly, the delete_employee() function receives an employee id and deletes the mentioned employee from the database.

It is important to note that in the update_employee() function, we add an execution parameter called synchronize_session. This method tells the session to “refresh” the updated entries using the fetch method, ensuring that all data in the memory will be up-to-date with the newly updated employee values.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a screenshot of the asynchronous service's source code for the get_employees and update_employees methods

Now that our database is ready, we can create an endpoint for our management service.

How to create an API endpoint with FastAPI?

Let’s start by creating a new file named app.py and importing symbols from the necessary packages:

We then create an instance of FastAPI that will handle all the endpoints of our application:

Next, we create a startup function using the @app.on_event("startup") decorator, that will run when we start our application:

The startup() function initializes our FastAPI and creates an instance of our database. This is done by either reading an already existing instance of employees.db or creating a new instance of our database.

All endpoints we make will asynchronously create a DB session and an EmployeeDAL instance with the session and then call their respective DAL functions. The main endpoint we are making is called employees and will have three methods. So, let us look at how we can create different methods for our employees endpoint.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a screenshot of Python code accessing some data in a database

What Will the GET Method Be?

The GET method for our employee management service will request the database to return all the employees:

The GET method creates an asynchronous session using async_session(). It then creates an instance of EmployeeDAL and later uses the get_employees() DAL function to get all of the employees in our database.

What Will the POST Method Be?

The POST method will serve to add an employee to our database. To do this, we will utilize the create_employee() DAL function that we created previously. This method’s response will be the details of the successfully added employee.

What Will the DELETE Method Be?

The DELETE method will help us delete employees from our database. We will do this by the delete_employee() DAL function. The method will return the id of the deleted employee.

What Will the PUT Method Be?

Finally, we create a PUT method that will allow us to update an existing employee in our database. This is done by calling the update_employee() DAL function in an EmployeeDAL instance. This method will simply return the id of the employee whose details have just been updated successfully.

How to Run the Server?

Finally, to run our server, we will run our app using uvicorn:

The code above starts our FastAPI application using the uvicorn ASGI server on port 8000 with our custom endpoint.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a screenshot of Python code being debugged

Here is what our final server file (app.py) looks like:

So, to run the server, simply execute your app.py python file. This will start an instance of your FastAPI application, ready to receive any API calls.

How to utilize a Python service as a client?

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a desktop screen showing the FastAPI endpoints

There are several ways to make API calls; we will explain them briefly below.

What is Swagger?

When you visit http://127.0.0.1:8000/docs in your browser, an automatic API documentation with all of the endpoints will appear. This is the Swagger UI.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a close up of some FastAPI endpoints

The Swagger UI is automatic, interactive API documentation created while creating your custom FastAPI endpoints. Here you will have options to make calls to your API’s various endpoint methods.

For example, you can make a POST call to add an employee to your database:

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI defining the CreateEmployee endpoint

Once you execute this, you will get a similar response as below:

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI tesing the end point using curl

As you can see, the response object returns a JSON that provides all the attributes of the newly added employee in your database.

What are query parameters?

As shown in the above screenshots of Swagger Docs, all the attributes we provide to swagger while making API calls are query parameters.

Other function parameters declared but not included in the path parameters are automatically treated as “query” parameters.

In addition to the Swagger UI, we can use traditional query parameters to send requests to our service. The query is the group of key-value pairs that come after the ? in a URL and are separated by & characters.

For example, a URL representing a POST call to add an employee:

How to use the requests Python package to create a client for an API?

Finally, you can also use the HTTP protocol to send client requests to the server, retrieve specific data, and also information on whether the transaction was successful.

Python has many packages that allow you to send and receive data from your client and server, but for this example, we will use the Python’s requests package to send and examine exchanges between the client and server.

Let’s install the requests package and confirm it’s installation:

To create a client endpoint using HTTP protocols, first, create a new file named simple_client.py import the requests package in your code:

Next, configure your base URL according to your server’s uvicorn running instance:

You can now send a GET request using get() method of requests package. This will fetch all the retrieved data using our API call:

Similarly, you can send a POST request using the requests.post method, but this time defining the query parameters:

Though we can communicate with our service manually, this is quite time-consuming. Thus, the best way to exchange information between the client and the server is through a GUI. We will discuss this later in the article with one of the best options for GUI in Python.

Why should you use PyScripter to create APIs?

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI the Pyscripter download screen

An integrated development environment (IDE) can be the difference between a good and bad programming experience. Moreover, a good IDE simplifies code development management, saving time and frustration, and making coding an overall good experience. This is where PyScripter outperforms its competitors.

PyScripter is currently the best IDE, and it started as a simple IDE to supplement the excellent Python for Delphi (P4D) components by providing a reliable scripting solution for Delphi applications. It has a more modern user interface and is faster than some other IDEs because it is written in a compiled language. It also has several features that make it an excellent Python development environment.

This amazing IDE includes handy features such as brace highlighting, code folding, code completion, and syntax checking as you type. It includes a Python interpreter that allows users to run Python programs without saving them. Finally, this IDE accepts files dropped from Explorer or other browsers applications, which could save you time making it an ideal IDE for developing and testing APIs.

How can we build a Python GUI for this API?

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI the Embarcadero DelphiVCL and DelphiFMX Python library landing page

Making a simple graphical user interface (GUI) can be difficult, but it doesn’t have to be. Many Python libraries allow you to create GUIs; however, none are comparable to the DelphiFMX package, which allows you to create attractive user interfaces that you and your users will appreciate.

What is DelphiFMX?

DelphiFMX is a Python module that is natively compiled and powered by the Python4Delphi library. This cross-platform framework is freely redistributable and provides Python developers access to the FireMonkey GUI framework. It is versatile and provides a robust structure for Windows, Linux, macOS, and Android.

How can we use the DelphiFMX library to create a stunning Python GUI?

The DelhpFMX package for Python allows us to create powerful and stunning GUIs with minimal code. But before creating a GUI for our service, we first have to install DelphiFMX. So, open up the terminal and run the following command:

Once installed, create a new Python file, eg. FMX_GUI_client.py, in your employee management service code directory.

Now, we can start building our service. First, import the relevant packages and initialize the BASE as the server URL:

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI another screenshot of a FastAPI endpoint

Next, we define an EmployeeManager class that will define our main form:

Here we are creating six labels: fname, lname, gender, role, year, and id using DelphiFMX’s Label class. We then create textboxes for each defined label using the Edit class. These textboxes will take input from the user to add some values to our application. Finally, we define three buttons addEmployee, view, and update, which will allow us to add employees to our database, view all the employees, or update the employee details in our database respectively. You can optionally use TColor to define the colors for your GUI.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a screenshot of the form show and form close methods along with some button click code

Now, we define private class methods that add functionality to our GUI and its buttons:

Lastly, we will define the main method that runs our GUI:

We can finally call our main function to run our GUI application. Optionally, you can add the following lines of code to run the GUI whenever the file is run:

What does our Python GUI look like?

Running the code will give the following GUI:

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a screenshot of the example GUI

If your server is running, then your GUI must be completely functional, and you can start making calls to your endpoints.

Clicking the View Employees button will display all the employees in your database:

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI the FastAPI example in action

We can see that the employee we added earlier using the swagger UI is in our database.

You can fill in all the employee details and add an employee to your database using the Add Employee button.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI the employee management screen

Below, you can see that an employee was successfully added to the database:

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI getting back results

Similarly, you can even update an employee’s details using the Update Employee button. Let’s consider that Jane has changed her last name. So we will type her id in the Employee ID text box and then update the field that we need to.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI showing the JSON results

Note that, to make updates using the PUT call, giving any parameter other than Employee ID is optional.

Now let’s view the list of employees again using View Employees to see if the changes have been reflected:

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI the view employees button

We can see that the changes have been made! Now, let’s even test our async API’s delete functionality by deleting the employee using the Delete Employee button with id number 1.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI the delete employee method

We can see the success message and API request’s appropriate response, but let’s view employees one last time to ensure the deletion:

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI testing the employee endpoints

Are you ready to create your own APIs using FastAPI and Python GUI?

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a laptop on the download PyScripter page

Great job on making it this far! We have successfully built an employee management service, using FastAPI and SQLAlchemy, that maintains an organization’s employee data. New employees can be added here, while existing employee details can be viewed or updated. Since this service is an example of database management, it was the perfect use case for asynchronous programming.

Hopefully, this tutorial has given you a better understanding of DelphiFMX and how to use it with FastAPI to build apps that are production-ready by default and offer the finest developer experience.

Now that you’re ready, click here and start creating your own highly performant production-ready applications with FastAPI and the best GUI framework in the market.

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI an image with a link to download a free ebook and styles bundle for Python GUI

What are some FAQs about APIs related to creating a Python GUI and asynchronous service?

How To Build An Asynchronous Employee Management Service Using FastAPI And Python GUI a laptop showing the PyScripter help screen

What are some benefits of asynchronous programming?

Using async programming improves performance, responsiveness, and user experience. With it, following page refreshes are no longer required because page load delays are eliminated.

Furthermore, even if other requests are running, you can use more than one feature at a time. Also, asynchronous applications use few resources and are very scalable, and the time it takes to respond to one request has no bearing on the time it takes to respond to others.

Another important aspect is that when one thread fails, the others keep rendering. It is also beneficial that the developer can make custom error messages using the built-in callback.

What is Embarcadero?

Embarcadero tools are designed for the world’s most elite developers who create and maintain the world’s most critical applications.

Embarcadero specializes in building business-critical applications in a demanding vertical scaling. It allows users to write steadfast code quickly and pass stringent code reviews faster than any other. In addition, it provides help from elite developers who understand C++ and Delphi’s scalability and stability and rely on the decades of innovation those languages bring to development.

Why is DelphiVCL famous in Python GUI?

DelphiVCL for Python is a widely known and recognized Python GUI framework geared toward native Windows development. It uses the Windows-only VCL framework and brings a powerful, flexible GUI framework to Windows, Linux, macOS, and Android.

What makes FastAPI one of the best libraries for creating apps in Python?

Many reasons make FastAPI one of the best APIs for creating production-ready applications. The first is the speed of development. Autocompletion and type hints in your favorite IDE or code editor are available in FastAPI. This is because the framework was designed to reduce the developer’s workload. Additional plugins, such as Flask-Marshmallow, are not required for FastAPI validation and serialization.

Secondly, the speed of execution: is fast too. FastAPI is capable of async execution. The server and toolkit it is based on are thanks to Uvicorn and Starlette. Flask lacks that capability as of now, giving FastAPI an advantage.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

5 Comments

Leave a Reply to Eduardo Jurschan Cancel reply

Your email address will not be published. Required fields are marked *