Hello World!

This explanation assumes that you have installed Reahl in a virtualenv, and that you have activated the virtualenv.

Run it

You can get a local copy of this example by using the reahl script and then run the example by doing:

reahl example tutorial.hello
cd hello
reahl setup develop -N
reahl createdbuser etc
reahl createdb etc
reahl createdbtables etc
reahl serve etc

Then browse to http://localhost:8000.

There is more to know about running examples. For now, you can see what other examples are available by running:

reahl listexamples

Source code


from reahl.web.fw import UserInterface
from reahl.web.ui import HTML5Page, P

class HelloPage(HTML5Page):
    def __init__(self, view):
        super().__init__(view)
        self.body.add_child(P(view, text='Hello World!'))

class HelloUI(UserInterface):
    def assemble(self):
        self.define_view('/', title='Home', page=HelloPage.factory())

In Reahl, each URL a user can visit is defined by a View, and a bunch of related Views are organised into a UserInterface.

  • The application consists of only one UserInterface: HelloUI.

  • HelloUI contains a single View, tied to the ‘’/’’ URL.

  • The URLs (and thus Views) defined by a UserInterface are set up in its .assemble() method.

  • HelloPage is an HTML5Page, which is a Widget like everything else on a Reahl user interface.

  • To give HelloPage some contents, just add a paragraph of text (P) to the .body of the page.

  • In the definition of the View, use a WidgetFactory for your HelloPage instead of constructing an actual HelloPage: the WidgetFactory is used to create a HelloPage, but only once that URL is visited.

The ‘hello’ component

The ‘hello’ project is a Reahl component.

Warning

The word component is used by different people for different things.

Create a component by putting all your code into a directory containing a .reahlproject file with info about the component.

The directory structure of hello:

hello/
├── etc/          - A directory for configuration files
├── hello.py      - A Python file containing the source of the app
└── .reahlproject - Metadata about this component

The .reahlproject file contains info about the component. To start, just list the other components that this one depends on (like component import statements):

<project type="egg">
  <deps purpose="run">
    <egg name="reahl-component"/>
    <egg name="reahl-web"/>
    <egg name="reahl-sqlalchemysupport"/>
    <egg name="reahl-web-declarative"/>
  </deps>
</project>

Development mode

Components are setuptools projects with a .reahlproject file from which we generate a standardised setup.py file.

When you are still busy developing a project, you must install it in “development mode”. From within the newly created hello directory, run:

reahl setup develop -N

This does a python setup.py develop -N using the .reahlproject file.

Configuration

You need one config setting to be able to run an application. In the config directory (etc), add a file web.config.py:

hello/
├── etc/
│   └── web.config.py
├── hello.py
└── .reahlproject

Config files contain Python code, but you can use dotted notation to access settings:

from hello import HelloUI

web.site_root = HelloUI

Each component you use has its own config file in etc. Most are optional. To see what is missing, do:

reahl listconfig --missing --files --info etc

Here’s example output:

Listing config for ./etc
web.site_root       web.config.py    The UserInterface class to be used as the root of the web application

You can see info about all the configuration settings used by executing:

reahl listconfig --files --info etc

Or, to see the other possibilities related to listconfig, use:

reahl listconfig -h etc

Prepare the database

Even a Hello World app needs a database. To create it, do:

reahl createdbuser etc

reahl createdb etc

reahl createdbtables etc

Note

Ignore the warnings about “dangerous” default config settings for now. They are meant to alert you to not use these settings in a production environment.

Development web server

To run your application just execute the following from the main hello directory:

reahl serve etc

The web server monitors the current directory recursively, and restarts when a file change is detected. For more options, see:

reahl serve -h