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

Pre-requisites

  • Python 3.6
  • pip

Setup

  • in an empty folder run python -m venv venv to create a new virtual environment then source ./venv/bin/activate to activate it.
  • pip install masonite to install masonite
  • confirm it is installed by running craft (craft is the command we'll use to trigger all of masonites generators and tools)
  • run craft new to generate a new project in the folder you are in.
  • run craft serve to run the server which defaults to port 8000

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.

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

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"),
]
"""Web Routes."""from masonite.routes import Get, PostROUTES = [
Get("/", "WelcomeController@show").name("welcome"),
Get("/first", "firstController@show").name("first"),
]

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 Coder

59 Followers

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