Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 5, 2019 04:41 pm GMT

Bocadillo: Yet Another Python Framework

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?

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

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

You can find Bocadillo on GitHub: 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

pip install bocadillo-cli

Framework Installation

pip install bocadillo

Using virtualenv

mkdir my_first_appcd my_first_appvirtualenv .source bin/activatepip 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

touch app.py

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

# app.py filefrom bocadillo import Appapp = 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.

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;

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.

from asyncio import sleepfrom bocadillo import Appapp = 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.*

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.

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.

import timefrom bocadillo import Appapp = 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/

Thanks for reading.


Original Link: https://dev.to/aligoren/bocadillo-yet-another-python-framework-57ii

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To