pyfyre package

class pyfyre.EventState(event_type: str, target: Optional[Element] = None)

Bases: StateDependency

A state which updates whenever the specified event_type is delivered to the target. If the target is None, will listen to the window instead.

This object is used as a state dependency for elements.

value

Brython DOMEvent type. The event delivered to the target. None if the event hasn’t been delivered yet.

Type:

Optional[DOMEvent]

Example:

Element(
    "p",
    lambda: [Text()],
    # The element will rebuild every time the window is scrolled.
    states=[EventState("scroll")]
)
class pyfyre.MediaQuery(media_query_string: str)

Bases: StateDependency

A state which updates whenever the media query match has changed its state.

This is similar to JavaScript window.matchMedia.

This object is used as a state dependency for elements.

Parameters:

media_query_string – Specifies the media query string to match.

Example:

Element(
    "p",
    lambda: [Text()],
    # The element will rebuild every time the media query matches.
    states=[MediaQuery("(max-width: 700px)")]
)
property matches: bool

True if the window currently matches the media query, False otherwise.

pyfyre.PRODUCTION: bool = False

Whether the app is running on production environment or not.

class pyfyre.State(value: T)

Bases: Generic[T], StateDependency

An object which stores a value of generic type T.

This object is used as a state dependency for nodes.

Example:

state = State(0)
state = State[int](0)  # With generic type.

Element(
    "p",
    lambda: [Text(f"Count: {state.value}")],  # It uses the value of the ``state``.
    states=[state]  # Set the ``state`` as a state dependency for this element.
)

state.set_value(1)  # The element will rebuild since it is depending on the ``state``.
set_value(value: T) None

Set the value and notify the dependers of this state object.

If the value is the same, nothing will happen.

property value: T

The value of this state object.

class pyfyre.Style(**props: str)

Bases: EventMixin

Provides styling to elements.

Parameters:

**props – CSS properties but the hyphens (-) are replaced by underscores (_). The values are unchanged.

props

Packed form of the **props argument.

Type:

Dict[str, str]

Example:

theme = Style(background_color="gray")

Element(
    "p",
    lambda: [Text("PyFyre is amazing!")],
    styles=[theme]  # Sets the background color of the element to gray.
)

theme["background_color"]  # Returns "gray".
theme["background_color"] = "black"  # Change the background color of the element to black.
del theme["background_color"]  # Remove the background color styling of the element.
css() str

CSS representation of this object.

classmethod from_styles(styles: List[Style], *, dynamic: bool = True) Style

Construct a Style object from multiple Style objects. The styles are evaluated in order from left to right.

Parameters:

dynamic – If True, the constructed Style object will listen to the changes of the styles. That means if one of the styles is updated, the constructed Style object will also get updated. If False, the constructed Style object will not listen to the changes of the styles.

set(**props: str) None

Overwrite the properties of this style.

Parameters:

**props – CSS properties but the hyphens (-) are replaced by underscores (_). The values are unchanged.

update(**props: str) None

Update the properties of this style.

Parameters:

**props – CSS properties but the hyphens (-) are replaced by underscores (_). The values are unchanged.

pyfyre.render(routes: Dict[str, Callable[[...], Node]]) None

The main function for initializing a PyFyre application. This function can only be called once.

Parameters:

routes – Pairs of route name and its corresponding route builder. The route builder returns a Node that will be rendered on the web page. The route builder optionally accepts a single argument. The value of the argument may be passed in by a RouterLink.

Raises:

PyFyreException – When this function is called again.

Example:

RouterLink("/random", arg="A Random Title")

render({
    "/": lambda: HomePage(),
    "/about": lambda: AboutPage(),
    "/contact": lambda: ContactPage(),
    "/random": lambda title: RandomPage(title)  # The argument defaults to None.
})

Subpackages

Submodules

pyfyre.events module

class pyfyre.events.PyFyreEventType(value)

Bases: Enum

PyFyre event types.

pyfyreload = 'pyfyreload'
pyfyre.events.window_event_listener(event_type: str) Callable[[Callable[[DOMEvent], None]], Callable[[DOMEvent], None]]

A decorator function.

Calls the decorated function whenever the specified event_type is delivered to the window.

Example:

@window_event_listener("error")
def onerror(event: DOMEvent) -> None:
    ...

pyfyre.exceptions module

exception pyfyre.exceptions.FutureAlreadyDone

Bases: FutureElementException

exception pyfyre.exceptions.FutureCancelled

Bases: FutureElementException

exception pyfyre.exceptions.FutureElementException

Bases: PyFyreException

exception pyfyre.exceptions.FutureNoResult

Bases: FutureElementException

exception pyfyre.exceptions.PyFyreException

Bases: Exception

pyfyre.router module

class pyfyre.router.RouteManager

Bases: object

A static class that enables navigation between various views in a PyFyre application.

static change_route(route_name: str, *, arg: Optional[Any] = None, force_build: bool = True) None

Change the current route.

This adds an entry to the browser’s session history stack.

If the route_name is not present in the ROUTES in your settings.py file, 404 Not Found will be returned.

If the ROUTES[route_name]["head"] (head data) is not equal to the head data of the current route, the page will reload. This is because it is really not allowed to modify the head tag in HTML.

Parameters:
  • route_name – The name of the route that will be rendered.

  • arg – The argument that will be passed in to the route builder.

  • force_build – Whether to call the route builder even if it is already called before. By default, called route builders are cached.

static get_node(route_name: str, *, arg: Optional[Any] = None, force_build: bool = True, parse_route: bool = True) Optional[Node]

Call the corresponding route builder of the route_name and return its Node.

Parameters:
  • arg – The argument that will be passed in to the route builder.

  • force_build – Whether to call the route builder even if it is already called before. By default, called route builders are cached.

  • parse_route – Whether to call the RouteManager.parse_route method on the route_name.

Returns:

The returned Node of the corresponding route builder of the route_name. If the route doesn’t exist, the default will be returned which has a 404 message.

static parse_route(route_name: str) str

Parse the route_name to turn it into a valid route name.

Examples

home -> /home
contact/ -> /contact
about/this -> /about/this
https://pyfyre.app/ -> /
https://pyfyre.app/about -> /about

pyfyre.states module

class pyfyre.states.EventState(event_type: str, target: Optional[Element] = None)

Bases: StateDependency

A state which updates whenever the specified event_type is delivered to the target. If the target is None, will listen to the window instead.

This object is used as a state dependency for elements.

value

Brython DOMEvent type. The event delivered to the target. None if the event hasn’t been delivered yet.

Type:

Optional[DOMEvent]

Example:

Element(
    "p",
    lambda: [Text()],
    # The element will rebuild every time the window is scrolled.
    states=[EventState("scroll")]
)
class pyfyre.states.MediaQuery(media_query_string: str)

Bases: StateDependency

A state which updates whenever the media query match has changed its state.

This is similar to JavaScript window.matchMedia.

This object is used as a state dependency for elements.

Parameters:

media_query_string – Specifies the media query string to match.

Example:

Element(
    "p",
    lambda: [Text()],
    # The element will rebuild every time the media query matches.
    states=[MediaQuery("(max-width: 700px)")]
)
property matches: bool

True if the window currently matches the media query, False otherwise.

class pyfyre.states.State(value: T)

Bases: Generic[T], StateDependency

An object which stores a value of generic type T.

This object is used as a state dependency for nodes.

Example:

state = State(0)
state = State[int](0)  # With generic type.

Element(
    "p",
    lambda: [Text(f"Count: {state.value}")],  # It uses the value of the ``state``.
    states=[state]  # Set the ``state`` as a state dependency for this element.
)

state.set_value(1)  # The element will rebuild since it is depending on the ``state``.
set_value(value: T) None

Set the value and notify the dependers of this state object.

If the value is the same, nothing will happen.

property value: T

The value of this state object.

class pyfyre.states.StateDependency

Bases: ABC, EventMixin

Abstract base class for state dependencies used for rebuilding nodes.

pyfyre.styles module

class pyfyre.styles.Style(**props: str)

Bases: EventMixin

Provides styling to elements.

Parameters:

**props – CSS properties but the hyphens (-) are replaced by underscores (_). The values are unchanged.

props

Packed form of the **props argument.

Type:

Dict[str, str]

Example:

theme = Style(background_color="gray")

Element(
    "p",
    lambda: [Text("PyFyre is amazing!")],
    styles=[theme]  # Sets the background color of the element to gray.
)

theme["background_color"]  # Returns "gray".
theme["background_color"] = "black"  # Change the background color of the element to black.
del theme["background_color"]  # Remove the background color styling of the element.
css() str

CSS representation of this object.

classmethod from_styles(styles: List[Style], *, dynamic: bool = True) Style

Construct a Style object from multiple Style objects. The styles are evaluated in order from left to right.

Parameters:

dynamic – If True, the constructed Style object will listen to the changes of the styles. That means if one of the styles is updated, the constructed Style object will also get updated. If False, the constructed Style object will not listen to the changes of the styles.

set(**props: str) None

Overwrite the properties of this style.

Parameters:

**props – CSS properties but the hyphens (-) are replaced by underscores (_). The values are unchanged.

update(**props: str) None

Update the properties of this style.

Parameters:

**props – CSS properties but the hyphens (-) are replaced by underscores (_). The values are unchanged.

pyfyre.utils module

class pyfyre.utils.EventMixin

Bases: object

A mixin class which implements event-like functionalities to the class inheriting this.

add_listener(listener: Callable[[], None]) None

Add the listener to this event.

call_listeners() None

Call all the listeners of this event.

remove_listener(listener: Callable[[], None]) int

Remove the listener to this event.

Returns:

The number of listeners removed, which is typically 1, unless the listener was added more than once.