Skip to content

Basics

Take your first steps with Nitro.

Hello World!

Call view() to show something on a page.

view('Hello World!')

Here, view() is comparable to Python's built-in print() function, and prints its arguments to the web page.

Screenshot

Formatting content

Strings passed to view() are interpreted as Markdown

view('_Less_ `code` means _less_ **bugs**.')

Screenshot

Show multiline content

Triple-quote strings to pass multiple lines of markdown.

view('''
The King said, very gravely:
- Begin at the beginning,
- And go on till you come to the end,
- Then stop.
''')

Screenshot

Show multiple items

Pass multiple arguments to view() to lay them out top to bottom.

view(
    'Begin at the beginning,',
    'And go on till you come to the end,',
    'Then stop.',
)

Screenshot

Show multiple items, one at a time

Call view() multiple times to show items one at a time.

The following example steps through three different pages.

view('Begin at the beginning,')
view('And go on till you come to the end,')
view('Then stop.')

Screenshot

Style text

To style text, wrap it in box(), and set style=. Nitro supports Tailwind styles.

view(
    box(
        'Hello world!',
        # p-4: Padding, 4 units wide.
        # border-l-4: Border on the left, 4 pixels thick.
        # text-sky-700: Dark blue text
        # bg-sky-50: Light blue background
        # border-sky-700: Dark blue border
        # text-sm: Small text
        # font-medium: Medium font
        style='p-4 border-l-4 text-sky-700 bg-sky-50 border-sky-700 text-sm font-medium',
    ),
)

view(text) is in fact shorthand for view(box(text)).

In general, box() can be used to create all kinds of content, like text blocks, dropdowns, spinboxes, checklists, buttons, calendars, and so on.

Screenshot

Get user input

Call box() with value= to create an input field and pass it to view().

When a view contains an input field, the view() function returns its input value.

# Display a textbox and assign the entered value to a variable.
name = view(box('What is your name?', value='Boaty McBoatface'))
# Print the entered value.
view(f'Hello, {name}!')

Here, view(box()) behaves similar to Python's built-in input() function.

Screenshot

Get multiple inputs, one at a time

Call view() multiple times to prompt for a sequence of inputs, one at a time.

The following example steps through three different pages.

# Prompt for first name.
first_name = view(box('First name', value='Boaty'))
# Prompt for last name.
last_name = view(box('Last name', value='McBoatface'))
# Print the entered values.
view(f'Hello, {first_name} {last_name}!')

Screenshot

Get multiple inputs at once

Pass multiple boxes to view() to prompt for inputs at once.

When a view contains multiple boxes, the view() function returns multiple values, in order.

# Prompt for first and last names.
first_name, last_name = view(
    box('First name', value='Boaty'),
    box('Last name', value='McBoatface'),
)
# Print the entered values
view(f'Hello, {first_name} {last_name}!')

Screenshot

Putting it all together

Views can be chained together to create sophisticated workflows and wizards.

The example below shows a simple online ordering system.

Observe how it combines view() with conditionals and loops, while keeping the code simple, concise, and clear.

Notably, if you have built web applications before, notice the absence of callbacks, event handlers, web request handlers, routing, etc.

# Our menu.
menu = dict(
    Donut=['Plain', 'Glazed', 'Chocolate'],
    Coffee=['Dark-roast', 'Medium-roast', 'Decaf'],
)

# Prompt for items.
items = view(
    '### What would you like to order today?',
    box(
        'Donuts, coffee, or both?',
        mode='check',
        options=list(menu.keys()),  # Menu item names.
    ),
)

if len(items) == 0:  # Nothing selected.
    view(f'Nothing to order? Goodbye!')
    return

# The order summary, which we'll display later.
summary = ['### Order summary:']

# Prompt for counts and flavors.
for item in items:
    count = view(
        f'### How many orders of {item} would you like?',
        box(f'{item} count', value=3),
    )
    for i in range(count):
        flavor = view(
            f'### Pick a flavor for {item} #{i + 1}',
            box(mode='radio', options=menu[item]),
        )
        summary.append(f'1. {flavor} {item}')

summary.append('\nThank you for your order!')

# Finally, show summary.
view('\n'.join(summary))

Building a similar multi-page interactive app with a regular web framework can be a fairly complex endeavor, weaving together requests and replies with logic spread across multiple functions , but Nitro makes all this delightfully simple!

Screenshot