As developers we often have the ability to choose our own tools to get the job done. If you’re like me, any time a new framework builds up a reputation for high performance, you jump right in to see what the developer experience is like. In today’s article, I want to look at building a web application with a Python ASGI framework called Starlette that promises blazing fast performance and developer productivity. We’ll put Starlette to the test by integrating MongoDBStitch, and Twilio into the mix to build an AirBnB-type property booking application.

If you would like to follow along with this tutorial, you can get the code from the GitHub repo. Also, be sure to sign up for a free MongoDB Atlas account to make it easier to connect your MongoDB database. Use code ADO200 to receive a $200 credit. In this tutorial, we’ll make use of a sample dataset that is provided by MongoDB Atlas.

What is Starlette

Starlette is a Python ASGI framework that shines. It is a fairly new framework but has already garnered the attention of many Python developers for its high performance, support for WebSocketsGraphQL, impressive 100% test coverage, and zero hard dependencies.

Starlette LogoThe framework is very light and easy to work with. The really impressive thing about Starlette is that it can be treated either as either an entire framework, or just a toolkit, where components can be independently utilized in your existing framework or application. For this tutorial, we’ll treat Starlette as a framework and build our entire application with Starlette. For this tutorial, I’ll assume you are familiar with Python, but if you need a refresher, there’s no better place than the official Python tutorial.

Today’s Project: MongoBnB

Today we are going to be building a rental reservation system, similar to AirBnB, that we’ll conveniently name MongoBnB. Our application will display a list of featured properties, allow the user to sort by various parameters, view additional details of a selected property, and finally book and cancel reservations.

MongoBnBWe have a lot to do today, so let’s dive right into it!

Building Our Dataset

As I mentioned at the beginning of this article, we are going to use a sample dataset provided by MongoDB Atlas. If you don’t already have an account, sign up for free here (and use code ADO200for a $200 credit), and create a new cluster. Once you have a cluster up and running, whether it’d be new or existing, load the sample dataset by clicking on the three dots button to see additional options, and then select “Load Sample Dataset.”

Load Sample DatasetThe sample dataset provided by MongoDB Atlas will create a number of collections containing various datasets. Navigate to the Collections tab to see them all. All of the datasets will be created as separate databases prefixed with sample_ and then the name of the dataset. It may take a few minutes for all of the databases to be imported, but the one we care about for our application is called sample_airbnb. Open this database up and you’ll see one collection called listingsAndReviews. Click on it to see the data we will be working with.

Sample AirBnB DatasetThis collection will have over 5,000 documents, with each containing all the information we could ever want for a particular listing. For our application, we’ll only work with a subset of the data, but you will have lots of existing data to expand on in case you wish to add additional features beyond the scope of the tutorial. Look around the dataset and familiarize yourself with it a little bit. Run a few filter queries such as {"price": {"$lt":10}} or {"review_scores.review_scores_rating":100} to find some really affordable properties or some of the highest rated ones respectively.

Now that we have our dataset ready, let’s get to writing some Python.

Setting Up Our Starlette Application

To get started with Starlette, we’ll need to install it. Python’s pip package manager can help with this. Open up your terminal and run the command pip3 install starlette. Starlette by itself is not a server so we’ll need to install an ASGI server as well. There’s many options like uvicorndaphne, or hypercorn, and for our tutorial we’ll use uvicorn. If you don’t already have uvicorn installed, run the command pip3 install uvicorn and we’ll be off to the races.

While we’re installing dependencies, let’s install one final one that we’ll need for our application and that is Jinja. Jinja is a templating engine for Python that we’ll use to render our application to the browser. Run pip3 install jinja2 to install it. This is an optional dependency for the Starlette framework, if you’re just planning on using Starlette to serve an API with JSON responses for example, then you could omit this step, but for our tutorial, we’ll make use of it.

Hello Starlette

Now that we have our dependencies installed, we’re ready to write some code. Create a new python file called app.py. This will be our main entry point into our Starlette application. To get started, let’s setup the most basic Starlette application to ensure that all of our dependencies were installed correctly. Write the following code:

1
2
3
4
5
6
7
8
9
10
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.responses import PlainTextResponse

async def homepage(request):
  return PlainTextResponse("Hello from Starlette")

app = Starlette(debug=True, routes=[
    Route('/', homepage),
])

copy code

The code above imports the Starlette framework, or various toolkit components if you will, such as routing and responses. Then on line 8 we create an instance of our Starlette application. For our app we’ll want the debugging to be on so we have detailed error reports, and for the second parameter we pass in our routes. For this test app, we’re only going to have one route and we’ll call it homepage. Finally, we implement this homepage route and all it does is display the message “Hello from Starlette” to the users browser.

Let’s make sure our app works. Since we installed uvicorn as our ASGI server, we’ll use it to start up our application. In the terminal execute the command uvicorn app:app in the directory where your app.py file resides. This will start up the default server on localhost:8000. You will get a message in the terminal saying that the Uvicorn server is running if everything went ok, if you have issues at this point, ensure that all of the above pip packages are installed. Check out the uvicorn docs page for troubleshooting.

If all went well and the server is up and running, navigate to localhost:8000 in your browser and you’ll see the message “Hello from Starlette.” Congrats! You just built your first Starlette application. Now let’s build something real.

#mongodb

Build a Property Booking Website with Starlette, MongoDB, and Twilio
2.80 GEEK