DEV Community

Python64
Python64

Posted on

Web2py hello world

Web2py is a free and open source web application framework for Python.
The web2py framework was inspired by the Ruby on Rails and Django frameworks. It does a lot of things for you.

Web2py uses the model–view–controller (MVC), where

  • the view is what is presented to the user (html)
  • the controller is Python code
  • the model is database stuff.

If this is unfamiliar, don't worry about it for now.
Do you want to make a web app (hello world) with Python? Lets do it!

Create new app

Creating a Python app with web2py is mostly click work. It's not all that hard. Register on PythonAnywhere, as we'll work online not on localhost for this tutorial.

First add a new web app. Click on the button "open web tab", which will take you to the next screen.

open web tab

Then click add a new web app. Your web apps will be shown on the right, if you have created some before.

add a new web app

Select web2py from the list. These are all Python web frameworks you can choose from. Any is possible, but this tutorial focuses on web2py

web2py framework

Then the directory is already defined, add an admin password. You may want to add a challenging password, because the whole admin panel is online.

add password

That easy! Now you have a web2py web app online. It even has an admin menu (think like wordpress admin, if you're familiar with that. But here we can type Python code)

Then you are at the web-admin admin screen

admin screen

Create hello world

Click on the blue admin button and login (your app has an admin panel). This takes you to the admin dashboard

click admin login

In this dashboard you can add apps (your web2py app can have apps)
Add a new app (on the right). It can be called "helloworld"

add new app

You'll then have a hello world app in the admin menu

add hello world app

Then you'll see your apps structure. It has the model-view-controller architecture. There are many files, each grouped in one of these 3 directories.

app folder

This comes with an online IDE. You can open the default controller, where you can change the yellow popup message. In every controller, app logic is dealt with. This will be where your Python code resides.

ide

But because we only want a "hello world" message, no code is required.

What is shown to the user is called a view. Open the view default/index.html

You can remove everything and just put "hello world". We don't need the hypertext markup language for displaying a message (unless you want to add an image or formatting)

template

Open your apps url and you'll see "hello world".

hello world in web2py

Top comments (0)