Web pages

Use a WebView to show a formatted document inside your Python script’s native interface. A recipe is a good fit when you want headings, an ingredient list and instructions on the same page.

A pancake recipe on iPhone A pancake recipe on iPhone
"""A pancake recipe with ingredient amounts for two or four servings."""

from dataclasses import dataclass

from juno import ui


STYLE = """
body { font: 1.2em/1.45 Georgia, serif; margin: 1.4em auto; max-width: 32em; padding: 0 1em; }
h2 { font: bold 1em system-ui; }
"""


@dataclass
class State:
    servings: int = 2


state = State()


def resize():
    state.servings = 4 if state.servings == 2 else 2


def body(s):
    html = f"""
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="color-scheme" content="light dark">
    <style>{STYLE}</style>
    <h1>Oat pancakes</h1><p>Serves {s.servings}</p>
    <h2>Ingredients</h2><ul><li>{50 * s.servings} g rolled oats</li>
        <li>{75 * s.servings} ml milk</li><li>{s.servings // 2} {"egg" if s.servings == 2 else "eggs"}</li></ul>
    <h2>Method</h2><p>Blend until smooth. Cook small spoonfuls in a lightly oiled pan.</p>
    """
    return ui.VStack(
        ui.Button("Cook for four" if s.servings == 2 else "Cook for two",
                  on_press=resize, style="bordered"),
        ui.WebView(html=html), spacing=12, width="fill", height="fill")


if __name__ == "__main__":
    ui.run(body, state, title="Recipe card")

Write the page in Python

Pass a string as html. This recipe includes all its text and styling in that string, so it works offline. Tap Cook for four and the script writes a new ingredient list with twice as much milk and oats. Inline HTML runs with JavaScript off.

Show a web address

To load a page from the web, pass an HTTP or HTTPS address as url and leave out html. Tapping a link in the document opens Safari over the script’s window. Keep the WebView outside a vertical ScrollView.

Parameters

Parameter

Default

Meaning

url

None

http:// or https:// address of a page, loaded with JavaScript on.

html

None

HTML text up to 64 KiB, shown with JavaScript off and no base address: scripts do not run, and relative links, images and local files do not load.

Common keywords

key, padding, disabled, accessibility_label, width, height, min_width, min_height, max_width, max_height. See Description and callback rules.

Seen in

Run News Reader from Home’s Native UI section and open a story to read its summary in a web view.