How to Work with Masonite — Python Rails/Laravel like Web Framework

We all love Ruby on Rails and how easy it makes creating APIs and websites. In python, Django has generally been the main batteries included framework used for many projects. The problem is Django has a lot of quirks that make its patterns quite different than the more railsesque approach frameworks for other languages have taken. Masonite provides a much more rails like experience in the Python language, let’s try it out!

Pre-requisites

Setup

Creating Some Routes

You’ll notice the layout of the folders is A LOT like the rails or Laravel folder structures. To add routes we need to create a controller, a class with methods that run when different requests are made to our server.

craft controller first

This creates a controller file in app/http/controllers with two methods.

It also includes the following method.

def show(self, view: View):
pass

Since we will be focusing on making a JSON api we don’t really need that View parameter (used for delivering html views). We can return a python dictionary or list and it will be treated as a json response.

def show(self):
return {"look": "I returned some json!"}

The methods name doesn’t matter beyond having some way to identify it, so you can call it what you want for now or just keep show.

Connecting the Controller to a Route

A route is a combination of a url endpoint and method that gets pointed to a particular controller function. This is handled in routes/web.py. We have an array of routes.

"""Web Routes."""from masonite.routes import Get, PostROUTES = [
Get("/", "WelcomeController@show").name("welcome"),
]

Essentially, the pre-existing route is saying that if the server receives a Get request to “/” the response is handled by the show function on the WelcomeController.

Let’s add a route for our controller.

"""Web Routes."""from masonite.routes import Get, PostROUTES = [
Get("/", "WelcomeController@show").name("welcome"),
Get("/first", "firstController@show").name("first"),
]

Make sure your server is running and head over to localhost:8000/first and you should see our json message! See, that wasn’t so hard!

Going Deeper into Masonite

Masonite has a world of tools available to you to migrate your database, generate your models, etc. It all uses patterns that shouldn’t feel unfamiliar from frameworks like Rails and Laravel. Try it out, I think you’ll like it!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Merced Coder

Alex Merced is a Developer Advocate for Dremio and host of the Web Dev 101 and Datanation Podcasts.