DEV Community

Cover image for Saying "Hello world" using Masonite Framework
Junior Gantin
Junior Gantin

Posted on

8 2

Saying "Hello world" using Masonite Framework

In this post, we will build a simple Hello World application using Masonite Framework.

What is Masonite Framework?

According to the official documentation, Masonite is describe as a Python web framework that strives for an actual batteries included developer tool with a lot of out of the box functionality with an extremely extendable architecture. Masonite is perfect for beginner developers getting into their first web applications as well as experienced developers.

Masonite uses the power of Python to offer developers a great set of features such as a simple and expressive routing engine, a powerful command line helpers, a simple migration system, a great Active Record style ORM, a great filesystem architecture for navigating and expanding your project and many more.

Let's begin

In order to use Masonite Framework, you’ll need:

  • Python 3.4+
  • Pip3 or Pipenv

You can now install Masonite using pip:

$ pip install masonite-cli
Enter fullscreen mode Exit fullscreen mode

The next step is to create the Masonite project using craft command line tool:

$ craft new hello_world
Enter fullscreen mode Exit fullscreen mode

This will get the latest Masonite project template inside a folder with the name hello_world. Now run the following commands:

$ cd hello_world
$ craft install
$ craft serve
Enter fullscreen mode Exit fullscreen mode

Open your browser and visit the following address:

http://localhost:8000/
Enter fullscreen mode Exit fullscreen mode

Voila!
Masonite Framework

Masonite is a truly MVC framework. All routes that define which action of which controller will serve are located in routes/web.py.

Now, add the following line in ROUTES list:

Get().route('/home', 'HomeController@home')
Enter fullscreen mode Exit fullscreen mode

In Masonite, you can define a Controller method to a route. Let's create HomeController. Run this command:

$ craft controller Home
Enter fullscreen mode Exit fullscreen mode

All controllers are located in the app/http/controllers directory. The HomeController generated contains:

''' A Module Description '''

class HomeController:
    ''' Class Docstring Description '''

    def show(self):
        pass

Enter fullscreen mode Exit fullscreen mode

Just rename show method and return a view:

class HomeController:

    def home(self):
        return view('home')

Enter fullscreen mode Exit fullscreen mode

view is called a helper function that do not require any imports and are simply just available 🔥. Let's create our home template with craft.

$ craft view home
Enter fullscreen mode Exit fullscreen mode

This will create resources/templates/home.html. Change its content:

<h1>Masonite is awesome!</h1>
Enter fullscreen mode Exit fullscreen mode

Open your browser at http://localhost:8000/home and there it is:
Hello world

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (1)

Collapse
 
almokhtar profile image
almokhtar bekkour

hhhhhh this is first time i hear about this framework , it 100% inspired from laravel hhahah

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay