Skip to content

new_app

Create a new view app.

Parameters:

Name Type Description Default
start bool

Should the app be started automatically? (In a new thread)

False
config_path Path | str | None

Path of the target configuration file

None
config_directory Path | str | None

Directory path to search for a configuration

None
post_init Callback | None

Callback to run after the App instance has been created

None
app_dealloc Callback | None

Callback to run when the App instance is freed from memory

None
store_address bool

Whether to store the address of the instance to allow use from get_app

True
Source code in src/view/app.py
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
def new_app(
    *,
    start: bool = False,
    config_path: Path | str | None = None,
    config_directory: Path | str | None = None,
    post_init: Callback | None = None,
    app_dealloc: Callback | None = None,
    store_address: bool = True,
) -> App:
    """Create a new view app.

    Args:
        start: Should the app be started automatically? (In a new thread)
        config_path: Path of the target configuration file
        config_directory: Directory path to search for a configuration
        post_init: Callback to run after the App instance has been created
        app_dealloc: Callback to run when the App instance is freed from memory
        store_address: Whether to store the address of the instance to allow use from get_app
    """
    config = load_config(
        path=Path(config_path) if config_path else None,
        directory=Path(config_directory) if config_directory else None,
    )

    app = App(config)

    if post_init:
        post_init()

    if start:
        app.run_threaded()

    def finalizer():
        if "_VIEW_APP_ADDRESS" in os.environ:
            del os.environ["_VIEW_APP_ADDRESS"]

        if app_dealloc:
            app_dealloc()

    weakref.finalize(app, finalizer)

    if store_address:
        os.environ["_VIEW_APP_ADDRESS"] = str(id(app))
        # id() on cpython returns the address, but it is
        # implementation dependent however, view.py
        # only supports cpython anyway

    return app