Skip to content

Configuration

Introduction

Before you can make any projects with view.py, you should learn about how it handles configuration. Configuration is handled by the configzen library under the hood, so most questions about configuration will be answered there.

The Config File

When creating your app, view will search for one of the following configuration files:

  • view.toml
  • view.json
  • view.ini
  • view_config.py

Note that while all of these are different formats, they can all evaluate to the same thing internally. If you have any questions on these semantics, once again see configzen.

Programatically

Many Python users aren't fond of the configuration file strategy, and that's okay. View supports editing the config at runtime just fine through the config property. The config property stores a Config object, which holds more subcategories.

app = new_app()
app.config.foo.bar = "..."

view.config.Config

Bases: ConfigModel

Source code in src/view/config.py
class Config(ConfigModel):
    dev: bool = True
    app: AppConfig = ConfigField(default_factory=AppConfig)
    server: ServerConfig = ConfigField(default_factory=ServerConfig)
    log: LogConfig = ConfigField(default_factory=LogConfig)
    templates: TemplatesConfig = ConfigField(default_factory=TemplatesConfig)

Configurations are loaded at runtime by the load_config function. If you would like to use View's configuration file without creating an App, you may use it like so:

from view import load_config

config = load_config()

view.config.load_config(path: Path | None = None, *, directory: Path | None = None) -> Config

Load the configuration file.

Parameters:

Name Type Description Default
path Path | None

Path to get the configuration from.

None
directory Path | None

Where to look for the configuration.

None
Source code in src/view/config.py
def load_config(
    path: Path | None = None,
    *,
    directory: Path | None = None,
) -> Config:
    """Load the configuration file.

    Args:
        path: Path to get the configuration from.
        directory: Where to look for the configuration."""
    paths = (
        "view.toml",
        "view.json",
        "view.ini",
        "view.yaml",
        "view.yml",
        "view_config.py",
        "config.py",
    )

    if path:
        if directory:
            return Config.load(directory / path)
            # idk why someone would do this, but i guess its good to support it
        return Config.load(path)

    for i in paths:
        p = Path(i) if not directory else directory / i

        if not p.exists():
            continue

        if p.suffix == ".py":
            spec = importlib.util.spec_from_file_location(str(p))
            assert spec, "spec is none"
            mod = importlib.util.module_from_spec(spec)
            assert mod, "mod is none"
            sys.modules[p.stem] = mod
            assert spec.loader, "spec.loader is none"
            spec.loader.exec_module(mod)
            return Config.wrap_module(mod)

        return Config.load(p)

    return Config()

Settings

View has several different configuration settings. For documentation purposes, values will be talked about in terms of Python (i.e. null values will be regarded as None).

At the top level, there's one real setting: dev.

dev is True by default, and is what tells view.py whether you're running in a production server setting or just running on your local machine.

Environment Variables

If you would like to set a configuration setting via an environment variable, you must account for the setting's environment prefix.

All environment prefixes look like view_<subcategory>_. For example, the loader setting is under the app section, so to set loader you would use the following command:

$ export view_app_loader=filesystem

App Settings

Environment Prefix: view_app_

  • loader: This is the strategy that will be used to load routes. Can be manual, simple, or filesystem. manual by default.
  • app_path: A string defining the location of the app, as well as the variable name. Should be in the format of file_path:variable_name. app.py:app by default.
  • uvloop: Whether or not to use uvloop as a means of event loop. Can be decide or a bool value. decide by default.
  • loader_path: When the loader is simple or filesystem, this is the path that it searches for routes. routes/ by default.

Example with TOML:

[app]
loader = "filesystem"
loader_path = "./app"

Server Settings

Environment Prefix: view_server_

  • host: IPv4 address specifying what address to bind the server to. 0.0.0.0 by default.
  • port: Integer defining what port to bind the server to. 5000 by default.
  • backend: ASGI backend to use. Only uvicorn is supported as of now.
  • extra_args: Dictionary containing extra parameters for the ASGI backend. This parameter is specific to the backend (only uvicorn, as of now) and not view.

Example with TOML:

[server]
host = "localhost"
port = 8080

Log Settings

Environment Prefix: view_log_

  • level: Log level. May be debug, info, warning, error, critical, or an int. This is based on Python's built-in logging module. info by default.
  • hijack: This is a bool value defining whether or not to "hijack" the ASGI backend's logger and convert it to view.py's logging style. True by default.
  • fancy: Whether to use View's fancy output mode. True by default.
  • pretty_tracebacks: Whether to use Rich Exceptions. True by default.

User Logging Settings

Environment Prefix: view_user_log_

  • urgency: The log level for user logging. info by default.
  • log_file: The target file for outputting log messages. None by default.
  • show_time: Whether to show the time in each message. True by default.
  • show_caller: Whether to show the caller function in each message. True by default.
  • show_color: Whether to enable colorization for messages. True by default.
  • show_urgency: Whether to show the urgency for messages. True by default.
  • file_write: The preference for writing to an output file, if set. May be both, to write to both the terminal and the output file, only, to write to just the output file, or never, to not write anything.
  • strftime: The time format used if show_time is set to True. %H:%M:%S by default.

Example with TOML:

[log]
level = "warning"
fancy = false

[log.user]
log_file = "app.log"

Template Settings

Environment Prefix: view_templates_

  • directory: The path to search for templates. ./templates by default.
  • locals: Whether to include local variables in the rendering parameters (i.e. local variables can be used inside templates). True by default
  • globals: The same as locals, but for global variables instead. True by default.
  • engine: The default template engine to use for rendering. Can be view, jinja, django, mako, or chameleon. view by default.

Example with TOML:

[templates]
directory = "./pages"
engine = "jinja"