Discover Masonite A Python Web Framework with Elegant API and Features

Introduction to Masonite

Masonite is an elegant, developer-centric Python web framework that empowers developers to build modern web applications effortlessly. Known for its simplicity, clarity, and speed, Masonite is a great choice for building performant and scalable applications.

Core Features of Masonite

Masonite comes packed with a wide range of APIs to simplify web development. Below, we dive into some of the key APIs and provide example code snippets.

Routing

Defining routes in Masonite is straightforward and flexible:

  from masonite.routes import Get, Post
  
  ROUTES = [
      Get('/', 'HomeController@show'),
      Post('/login', 'AuthController@login'),
  ]

You can also use route groups, middleware, and named routes for better control:

  from masonite.routes import RouteGroup
  
  ROUTES = [
      RouteGroup([
          Get('/dashboard', 'DashboardController@index'),
          Get('/settings', 'UserController@settings'),
      ], middleware=['auth'], name='dashboard')
  ]

Controllers

Controllers handle application logic. Here’s a simple example:

  from masonite.controllers import Controller
  from masonite.request import Request
  
  class HomeController(Controller):
      def show(self, request: Request):
          return 'Welcome to Masonite!'

Models

Masonite provides ORM capabilities through Orator.

  from masoniteorm.models import Model

  class User(Model):
      __fillable__ = ['name', 'email', 'password']

Create, read, update, and delete records easily:

  user = User.create(name="John Doe", email="john@example.com", password="password123")
  user = User.find(1)
  user.update(name="John Updated")
  user.delete()

Authentication

Masonite makes user authentication seamless with its Auth system:

  from masonite.auth import Auth
  from masonite.request import Request

  def login_user(request: Request):
      Auth(request).login_by_id(1)  # Logs in user with ID 1
      return 'User Logged In'

Middleware

Middleware is used for handling pre- and post-request logic:

  from masonite.middleware import Middleware

  class CustomMiddleware(Middleware):
      def before(self, request, response):
          print("Before Request")
      def after(self, request, response):
          print("After Request")

Task Scheduling

Masonite has a built-in scheduler for handling repetitive tasks:

  from masonite.scheduling import Kernel, Job

  Kernel.schedule(Job('example_task').every_hour())

Emailing

Sending emails is straightforward with Masonite:

  from masonite.mail import Mailable, Mail

  class WelcomeEmail(Mailable):
      def build(self):
          return self.to("user@example.com").subject("Welcome!").view("emails.welcome")

  Mail().send(WelcomeEmail())

Building a Simple Blog App

Here’s an example of a blog application utilizing Masonite’s APIs:

Step 1 Create Your Models

  from masoniteorm.models import Model

  class Post(Model):
      __fillable__ = ['title', 'content']

Step 2 Define Routes

  from masonite.routes import Get, Post

  ROUTES = [
      Get('/', 'PostController@index'),
      Get('/post/@id', 'PostController@show'),
      Post('/post', 'PostController@store'),
  ]

Step 3 Create a Controller

  from masonite.controllers import Controller
  from masonite.request import Request
  from app.models.Post import Post

  class PostController(Controller):
      def index(self):
          return Post.all()

      def show(self, request: Request):
          post_id = request.param("id")
          return Post.find(post_id)

      def store(self, request: Request):
          title = request.input("title")
          content = request.input("content")
          post = Post.create(title=title, content=content)
          return post

With this setup, you’ve successfully built a basic blog application using Masonite!

Start building with Masonite and experience the elegance of its APIs. The framework is designed to make web development seamless and intuitive.

Leave a Reply

Your email address will not be published. Required fields are marked *