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
python -m pip install --no-deps -e .
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 View
s 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
View
s) defined by aUserInterface
are set up in its.assemble()
method.HelloPage is an
HTML5Page
, which is aWidget
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 aWidgetFactory
for your HelloPage instead of constructing an actual HelloPage: theWidgetFactory
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.
We call a user interface element a
Widget
We use component to refer to a project that is packaged as a distribution package and which additionally uses the reahl component infrastructure.
Create a component by creating a setuptools package with ‘pyproject.toml’ and ‘setup.cfg’ files.
You did this above by running:
reahl example tutorial.hello
The directory structure of hello:
hello/
├── etc/ - A directory for configuration files
├── hello.py - A Python file containing the source of the app
├── pyproject.toml - The standard (PEP518) python build system configuration
└── setup.cfg - Normal configuration for setuptools
The pyproject.toml file should include as build dependencies: ‘setuptools, ‘toml’ and ‘reahl-component-metadata’:
[build-system]
requires = ["setuptools >= 51.0.0", "wheel","setuptools-git >= 1.1", "pytest-runner", "toml", "reahl-component-metadata"]
build-backend = "setuptools.build_meta"
The setup.cfg file contains info about the component. To start, just give your component a name, specify an empty component = option and list the requirements of your component:
[metadata]
name = hello
version = 0.1
[options]
py_modules =
hello
install_requires =
reahl-component>=6.0,<6.1
reahl-web>=6.0,<6.1
reahl-sqlalchemysupport>=6.0,<6.1
reahl-sqlitesupport>=6.0,<6.1
reahl-web-declarative>=6.0,<6.1
component =
More information on the Reahl component infrastructure is in its own introduction.
Development mode¶
Components are setuptools projects with some extra metadata added.
When you are still busy developing a project, install your project in “development mode”. From within the newly created hello directory, run:
python -m pip install --no-deps -e .
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
├── pyproject.toml - The standard (PEP518) python build system configuration
└── setup.cfg - Normal configuration for setuptools
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