Build a simple server using Robin Web Framework
“Robin is a fast, high-performance Python web framework with Rust runtime. It is designed to provide near-native Rust throughput while taking advantage of the code being written in Python. Robin is on par with web servers from other products such as Flask, FastAPI, Django and the likes. One of the major advantages of Robin is that it does not require an external web server for production, making it more efficient and streamlined.
More about Robin Here Shankar’s blog.
- Under active development!
- written in rust, btw xD
- a multithreaded runtime
- a simple api
- Sync and asynchronous function support
- Dynamic URL Routing
- multi-core scaling
- hot reloading
- Community first and truly FOSS!
py -m venv venv cd venv/Scripts activate
pip install robyn
Let’s write a simple endpoint that returns “hello world”. we create a file named
We import Robin and create a Robin instance. in this matter,
Then, we define a path operation decorator. when a request goes to the path
/ using a
get operation, the
hello The function will handle the request and “Hello, World!” Will respond with
To run the server, we write in our terminal:
here, we import
static file object, and inside the handler, we pass the path to the HTML file we want to serve.
this handler returns
fake_fruit_database List as a JSON.
get id by handler
helper.py Contains a function to help us look up a list and return a dictionary with associated
fruit/:id handler, we are adding the path parameter
idto bear fruit with
The form of the request object is:
- to find the value of
idwe passed the key
paramskey first and then
- if no
idis matched, the handler returns a “fruit not found” message. Otherwise, it returns a JSON response.
In the handler below, we extract the body from the request object and decode it:
then, we use
json.loads() Method to deserialize and convert the body to a python dictionary.
we generate a
id For fruit we are adding. We create a new dictionary and add it to the list of fruits. and return the status code
201 And a JSON as body response.
we need to get out
id The parameters and body for this handler.
Then, we search with the fruit
id passed and replaces the fruit value with the one in the request body.
Here, we remove the dictionary from the list of fruits
id passed as a parameter.
If we want to start the server on a different port and url, we need to pass the values
I’ve been learning Rust for a few months, and I thought it was interesting to use Rust to build a Python web framework and leverage the power of Rust. I was excited to use and write about Robin. And I’m excited to follow its development. Let me know if you have any recommendations about other packages, architectures, etc.
source code is available Here, Thank you for reading.