Unlock the Power of Python Web Development with Masonite
A Familiar Framework for a New Era
When it comes to Python web development, Django is often the go-to framework. However, its unique patterns and steep learning curve can be daunting for many developers. What if there was a framework that combined the best features of modern frameworks like Ruby’s Rails, PHP’s Laravel, and NestJS, while still using Python? Enter Masonite, a robust and familiar framework that’s changing the game.
Masonite: A Framework for the Modern Developer
Masonite offers a range of features that make it an attractive alternative to Django. With its IOC container and auto-resolving dependency injection, service providers that easily add functionality, and extremely simple static files configuration, Masonite is designed to make development easier and more efficient. Its ORM, similar to Active Record, and command line tool, craft, make it a powerful tool in any developer’s arsenal.
Building a To-Do List App with Masonite
In this tutorial, we’ll build a simple to-do list app using Masonite. To get started, you’ll need Python ≥v3.6 installed.
python -m venv myenv
source myenv/bin/activate
pip install masonite
craft new project todo
cd todo
Controllers, Actions, and Routes
In Masonite, controllers are classes that contain several methods called actions. Actions determine how to respond to particular requests, while routes are rules that determine which URL or method combinations should be directed to which controller actions. Let’s create our first controller and define a route to render a template.
from masonite.controller import Controller
class TodoController(Controller):
def show(self, view):
return view.render('helloworld', {'message': 'Hello, World!'})
ROUTE = [
Get().route('/', 'TodoController@show'),
]
Rendering Templates and Views
To render a template, we’ll use the view parameter that we passed into the show method. This tells Masonite to look for a template called helloworld.html and render it using the data in the dictionary that is passed as the second argument.
craft view helloworld
Sending JSON Data and Working with Migrations
Sending JSON data is straightforward in Masonite. We can return either a dictionary or an array in our action. To set up our application for migrations, we’ll configure the database details in the.env file and run a migration to create our SQLite3 database.
from masonite.response import Response
class TodoController(Controller):
def show(self, response: Response):
return response.json({'message': 'Hello, World!'})
#.env file
DB_CONNECTION=sqlite
DB_HOST=localhost
DB_PORT=
DB_DATABASE=todo
DB_USERNAME=
DB_PASSWORD=
craft migrate:install
craft migrate:run
Creating a Model and Adding To-Do Items
Now that we have a table for our Todos, we can create a model that allows us to interact with the data. We’ll create a file app/Todo.py with our model class and import it into our controller.
from masoniteorm.models import Model
class Todo(Model):
pass
Then, we’ll add some to-do list items in the Python shell and send back all those to-do list items as JSON.
from app.Todo import Todo
todos = Todo.all()
return response.json(todos)
CORS and Deploying to Heroku
Before we deploy our application, we need to set up our CORS headers in case we receive external requests to our application.
from masonite.middleware import CORS
CORSProvider = [
CORS(
allow_origins=['*'],
allow_methods=['GET', 'POST', 'PUT', 'DELETE'],
allow_headers=['Content-Type', 'Authorization'],
),
]
Then, we’ll deploy our Masonite to-do list application to Heroku by installing Gunicorn and Psycopg2, creating a Procfile and runtime.txt file, and pushing our project up to GitHub.
pip install gunicorn psycopg2-binary
echo "web: gunicorn 'todo.wsgi:application'" > Procfile
echo "python-3.9.5" > runtime.txt
git init
git add.
git commit -m "Initial commit"
git remote add origin
undefined.com/username/todo.git
git push -u origin master
heroku git:remote -a todo-app
git push heroku master