Skip to content

Project Creation

Automatic

The View CLI supports automatically creating a project via the view init command.

$ view init

Note

Depending on how or where you installed view.py, view might open vim! If that's the case, run python3 -m view init, or however your environment works.

Alternatively, you can run view init with pipx:

$ pipx run view-py init

Manually

view.py doesn't actually need any big project structure. In fact, you can run an app in just a single Python file, but larger structures like this might be more convenient for big projects. The only real requirement for something to be a view app is that it calls new_app, but again, more on that later.

Some "hello world" code for manually starting a view project would look like this:

from view import new_app

app = new_app()

@app.get("/")
def index():
    return "..."

app.run()

Structure

First, in any view project, you need a file to contain your app. By default, view expects it to be in app.py under a variable called app. Again, you can change this via the app_path setting. You're also going to want an app.run() (assuming you named your App instance app), but more on that later.

from view import new_app

app = new_app()
app.run()

view.app.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, config: Config | None = None, error_class: type[Error] = Error) -> 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
config Config | None

Raw Config object to use instead of loading the config.

None
error_class type[Error]

Class to be recognized as the view.py HTTP error object.

Error

Generally, you're going to want one of the configuration files talked about earlier, but if you're against configuration files that's OK, view.py will work just fine without it. If you choose to use something other than manual routing, you want a routes directory (unless you changed the loader_path setting).

# view.toml
dev = true

[app]
loader_path = "./my_custom_loader_path"

For mobility purposes, you may want to add a pyproject.toml that contains the dependencies for your project, in case you need to run your project on a different system.

# pyproject.toml
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "your_view_app"
requires-python = ">=3.8"
authors = [
  { name = "Your Name", email = "your@email.com" },
]
dependencies = ["view.py"]