Bocadillo: Yet Another Python Framework
Tags: python, async, framework
Published: 2019-06-05 16:41:43

Hi everyone. In this post, I'll introduce the Bocadillo framework. As you know, there are a lot of web frameworks in Python.

In the past days, I was looking for new technologies to improve myself. I saw Bocadillo on GitHub.

## What is Bocadillo?

![](https://thepracticaldev.s3.amazonaws.com/i/9hymvcqp618nt6n6pcd7.png)

Bocadillo is a modern Python web framework filled with asynchronous salsa.

You can access Bocadillo's website using this link: [https://bocadilloproject.github.io/](https://bocadilloproject.github.io/)

You can find Bocadillo on GitHub: [https://github.com/bocadilloproject/bocadillo](https://github.com/bocadilloproject/bocadillo)

Its key features are;

- Productive
- Real-time capable
- Flexible
- Performant
- Empowering
- Transparent

Transparent: All codes are developed with type-annotation.

You can also develop WebSocket apps. Actually, I think, its big claim is WebSockets.

## Installation

The Bocadillo has a CLI tool to create projects. But you don't need to install the CLI tool.

### CLI Installation

```sh
pip install bocadillo-cli
```

### Framework Installation

```sh
pip install bocadillo
```

### Using virtualenv

```sh
mkdir my_first_app

cd my_first_app

virtualenv .

source bin/activate

pip install bocadillo
```

Now, we installed the Bocadillo.

## First Bocadillo App

You can use favorite editor or IDE. I'll create a file called app.py

```sh
touch app.py
```

I'll edit app.py file with the **nano** editor. Yes this is hippie style :)

```python
# app.py file
from bocadillo import App

app = App()

@app.route("/")
async def index(req, res):
  res.text = "Hello Dev Family"
```

That's all. Bocadillo highly similar to the Flask framework. To run our first app, we'll use the [uvicorn](https://www.uvicorn.org/).

Don't worry it comes up with the Bocadillo. You don't need to install it again.

`uvicorn app:app --reload`

We'll run our first application using the above command. After this command you'll see an output like below;

```sh
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [24076]
INFO: Started server process [24078]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
```

This means, our web app works on port 8000. Good work, we created our first app. Let's change our app and use background tasks.

```python
from asyncio import sleep
from bocadillo import App

app = App()

@app.route("/")
async def index(req, res):

  try:
    if req.query_params["wait"] == "true":
      @res.background
      async def waitForTest():
        await sleep(15)
        print("I'll work :)")
  except:
    print(":/")

  res.text = "Hello Dev family"
```

Background tasks are a lightweight mechanism to perform processing after a request has been sent, without making the client wait for the result. Typical examples include sending email, or sending logs to a remote system.

When registered on the Response, the background task won't run immediately. Instead, the view will terminate as usual, and the response will be sent. Only then, the background task will execute.

This prevents clients from waiting for the background task to finish before getting a response.[*](https://bocadilloproject.github.io/guide/background-tasks.html)

If you try without background tasks, you need to wait for the sleep method finish. Firstly you should try the above code.

1-) Open two terminals.

Run this command in the first terminal.

`curl http://127.0.0.1:8000/?wait=true`

Immediately run this command in the second terminal.

`curl http://127.0.0.1:8000/`

You'll see "Hello Dev family" output in all terminals. You need also look at the original terminal which you run your first app.

![](https://thepracticaldev.s3.amazonaws.com/i/oon9y5xl1m8qotr88opm.png)

After 15 seconds, you'll see **"I'll work"** output.

If you don't use async background tasks you will need to wait for the sleep method finish.

```python
import time
from bocadillo import App

app = App()

@app.route("/")
async def index(req, res):

  try:
    if req.query_params["wait"] == "true":
      time.sleep(15)
      print("I'll work :)")
  except:
    print(":/")

  res.text = "Hello Dev family"
```

Don't forget, **Background tasks must be non-blocking.**. Because a background task is a coroutine, it must be non-blocking to avoid blocking the main thread and preventing requests from being processed.

That's all for now.

You should look at the Bocadillo Guide for the other features. I introduced the Bocadillo.

**Guide**: [https://bocadilloproject.github.io/guide/](https://bocadilloproject.github.io/guide/)

Thanks for reading.