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.
When creating your app, view will search for one of the following configuration files:
view.tomlview.jsonview.iniview_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.
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 = "..."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 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.
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=filesystemEnvironment variables can also be set via the env config setting, or by adding a .env file to the project:
[env]
TEST = "hello"TEST=helloYou can access environment variables via the view.env utility:
from view import env
test = env("TEST", tp=int)
# test will be an integer. if environment variable "TEST" does not exist, an exception is thrown.
# if environment variable "TEST" is not an integer, an exception is thrown.Environment Prefix: view_app_
| Key | Description | Default |
|---|---|---|
loader |
This is the strategy that will be used to load routes. Can be manual, simple, or filesystem. |
manual |
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 |
uvloop |
Whether or not to use uvloop as a means of event loop. Can be decide or a bool value. |
decide |
loader_path |
When the loader is simple or filesystem, this is the path that it searches for routes. |
routes/ |
Example with TOML:
[app]
loader = "filesystem"
loader_path = "./app"Environment Prefix: view_server_
| Key | Description | Default |
|---|---|---|
host |
IPv4 address specifying what address to bind the server to. 0.0.0.0 by default. |
0.0.0.0 |
port |
Integer defining what port to bind the server to. | 5000 |
backend |
ASGI backend to use. Can be uvicorn, daphne, or hypercorn. |
uvicorn |
extra_args |
Dictionary containing extra parameters for the ASGI backend. This parameter is specific to the backend and not View. | {} |
Example with TOML:
[server]
host = "localhost"
port = 8080Environment Prefix: view_log_
| Key | Description | Default |
|---|---|---|
level |
Log level. May be debug, info, warning, error, critical, or an int. This is based on Python's built-in logging module. |
info |
server_logger |
This is a bool determining whether the ASGI backend's logger should be displayed. |
False |
fancy |
Whether to use View's fancy output mode. | True |
pretty_tracebacks |
Whether to use Rich Exceptions. | True |
startup_message |
Whether to show the view.py welcome message on server startup. | True |
Environment Prefix: view_user_log_
urgency: The log level for user logging.infoby default.log_file: The target file for outputting log messages.Noneby default.show_time: Whether to show the time in each message.Trueby default.show_caller: Whether to show the caller function in each message.Trueby default.show_color: Whether to enable colorization for messages.Trueby default.show_urgency: Whether to show the urgency for messages.Trueby default.file_write: The preference for writing to an output file, if set. May beboth, to write to both the terminal and the output file,only, to write to just the output file, ornever, to not write anything.strftime: The time format used ifshow_timeis set toTrue.%H:%M:%Sby default.
Example with TOML:
[log]
level = "warning"
fancy = false
[log.user]
log_file = "app.log"Environment Prefix: view_templates_
directory: The path to search for templates../templatesby default.locals: Whether to include local variables in the rendering parameters (i.e. local variables can be used inside templates).Trueby defaultglobals: The same aslocals, but for global variables instead.Trueby default.engine: The default template engine to use for rendering. Can beview,jinja,django,mako, orchameleon.viewby default.
Example with TOML:
[templates]
directory = "./pages"
engine = "jinja"