





















































Hi ,
Welcome to a brand new issue of PythonPro!
In today’sExpert Insight we bring you an excerpt from the recently published book, FastAPI Cookbook, which explains how to deploy FastAPI apps using Docker, covering Dockerfile creation, image building, and container generation.
News Highlights: Numbast simplifies Python-CUDA C++ integration by auto-generating Numba bindings for CUDA functions; and DJ Beat Drop enhances Django’s new developer onboarding with a streamlined project initializer.
And, today’s Featured Study, introduces LSS-SKAN, a Kolmogorov–Arnold Network (KAN) variant that uses a single-parameter function (Shifted Softplus) for efficient accuracy and speed.
Stay awesome!
Divya Anne Selvaraj
Editor-in-Chief
P.S.:Thank you to those who participated in this month's survey. With this issue, we have tried to fulfill at least one request made by each participant. Keep an eye out for next month's survey.
httpx
to aiohttp
library resolved high-concurrency issues and improved stability in a computer vision application.orc
library to simplify fuzzy matching by providing a human-friendly interface that highlights edits and can invert changes, enhancing usability for complex text correction tasks.RelatedFieldWidgetWrapper
and a custom ModelForm
, allowing for consistent filtering on both sides of a ManyToMany relationship.boto3
and Athena SQL scripts provides customizable, cost-effective automation.In "LSS-SKAN: Efficient Kolmogorov–Arnold Networks based on Single-Parameterized Function," Chen and Zhang from South China University of Technology present a refined Kolmogorov–Arnold Network (KAN) variant. Their study introduces an innovative design principle for neural networks, improving accuracy and computational speed while ensuring greater model interpretability.
KANs are neural networks based on the Kolmogorov-Arnold theorem, which breaks down complex, multivariate functions into simpler univariate ones, aiding in better visualisation and interpretability. This makes them valuable in critical decision-making applications, where understanding a model's decision process is crucial. Unlike typical neural networks like Multilayer Perceptrons (MLPs), which rely on opaque linear and activation functions, KANs assign functions to network edges, creating a more interpretable structure. Over time, several KAN variants, such as FourierKAN and FastKAN, have emerged, each with unique basis functions to balance speed and accuracy.
LSS-SKAN builds on these advancements with the Efficient KAN Expansion (EKE) Principle, a new approach that scales networks using fewer complex basis functions, allocating parameters to the network's size instead. This principle is central to LSS-SKAN's efficiency and demonstrates how a simpler basis function can yield high accuracy with reduced computational cost.
For those working in machine learning or fields requiring interpretable AI, LSS-SKAN offers a practical solution to enhance neural network accuracy and speed while maintaining transparency in model decision-making. LSS-SKAN is particularly beneficial in applications involving image classification, scientific computing, or scenarios demanding high interpretability, such as medical or financial sectors where model explainability is crucial.
The researchers conducted detailed experiments using the MNIST dataset to measure LSS-SKAN’s performance against other KAN variants. They tested both short-term (10-epoch) and long-term (30-epoch) training cycles, focusing on two key metrics: accuracy and execution speed.
Through these tests, LSS-SKAN consistently outperformed other KAN models in accuracy, achieving a 1.65% improvement over Spl-KAN, 2.57% over FastKAN, and 0.58% over FourierKAN, while also running 502.89% faster than MLP+rKAN and 41.78% faster than MLP+fKAN.
The LSS-SKAN Python library is available on GitHub, along with experimental code, so you can replicate and build on their findings. They recommend a learning rate between 0.0001 and 0.001 for best results, particularly due to KANs’ sensitivity to learning rate adjustments.
You can learn more by reading the entire paper and accessing LSS-SKAN.
Here’s an excerpt from “Chapter 12: Deploying and Managing FastAPI Applications” in the book, FastAPI Cookbook by Giunio De Luca, published in August 2024.
Dockeris a useful tool that lets developers wrap applications with their dependencies into a container. This method makes sure that the application operates reliably in different environments, avoiding the commonworks on my machine issue. In this recipe, we will see how to make a
Dockerfile and run a FastAPI application inside a Docker container. By the end of this guide, you will know how to put your FastAPI application into a container, making it more flexible and simpler to deploy.
You will benefit from some knowledge of container technology, especially Docker, to follow the recipe better. But first, check thatDocker Engineis set up properly on your machine. You can see how to do it at thislink:https://docs.docker.com/engine/install/.
If you use Windows, it is better to installDocker Desktop, which is a Docker virtual machine distribution with a built-ingraphical interface.
Whether you have Docker Engine or Docker Desktop, make sure the daemon is running by typingthis command:
$ docker images
If you don’t see any error about the daemon, that means that Docker is installed and working on the machine. The way to start the Docker daemon depends on the installation you choose. Look at the related documentation to see how todo it.
You can use the recipe for your applications or follow along with theLive Application
application that we introduced in the first recipe, which we are using throughoutthe chapter.
It is not very complicated to run a simple FastAPI application in a Docker container. The process consists ofthree steps:
Then, you just have to run the container to have theapplication working.
The Dockerfile contains the instructions needed to build the image from an operating system and the file we wantto specify.
It is good practice to create a separate Dockerfile for the development environment. We will name itDockerfile.dev
and place it under the projectroot folder.
We start the file by specifying the base image, which will beas follows:
FROM python:3.10
This will pull an image from the Docker Hub, which already comes with Python 3.10 integrated. Then, we create a folder called/code
that will hostour code:
WORKDIR /code
Next, we copyrequirements.txt
into the image and install the packages insidethe image:
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir -r /code/requirements.txt
Thepip install
command runs with the--no-cache-dir
parameter to avoidpip
caching operations that wouldn’t be beneficial inside a container. Also, in a production environment, for larger applications, it is recommended to pin fixed versions of the packages inrequirements.txt
to avoid potential compatibility issues due topackage upgrades.
Then, we can copy theapp
folder containing the application into the image with thefollowing command:
COPY ./app /code/app
Finally, we define the server startup instructionas follows:
CMD ["fastapi", "run", "app/main.py", "--port", "80"]
This is all we need to create ourDockerfile.dev
file.
Once we haveDockerfile.dev
, we can build the image. We can do it by running the following from the command line at the project rootfolder level:
$ docker build -f Dockerfile.dev -t live-application .
Since we named our DockerfileDockerfile.dev
, we should specify it in an argument. Once the build is finished, you can check that the image has been correctly built by runningthe following:
$ docker images live-application
You should see the details of the image on the output printlike this:
REPOSITORY TAG IMAGE ID CREATED SIZE
live-application latest 7ada80a535c2 43 seconds ago 1.06GB
With the image built, we can proceed with creating thecontainer creation.
To create the container and run it; simply runthe following:
$ docker run -p 8000:80 live-application
This will create the container and run it. We can see the container by runningthe following:
$ docker ps -a
Since we didn’t specify a container name, it will automatically affect a fancy name. Mine, for example,isbold_robinson
.
Open the browser onhttp://localhost:8000
and you will see the home page response ofour application.
This is all you need to run a FastAPI application inside a Docker container. Running a FastAPI application in a Docker container is a great way to use the advantages of both technologies. You can easily scale, update, and deploy your web app withminimal configuration.
The Dockerfile can be used to specify several features of the image. Check the list of commands in the official documentation:
FastAPI Cookbook was published in August 2024.
And that’s a wrap.
We have an entire range of newsletters with focused content for tech pros. Subscribe to the ones you find the most usefulhere. The complete PythonPro archives can be foundhere.
If you have any suggestions or feedback, or would like us to find you a Python learning resource on a particular subject, just respond to this email!