Flama CLIRun
Flama CLI~ 5 min read

Run

Flama CLI

We have already seen how to use the command line to run an app in the Quickstart, but we didn't do a proper introduction of the command line interface (CLI). Flama CLI is a convenient tool which is installed automatically with the package, and reduces the hurdles of serving your app.

Once you have a developed app that you want to serve, you need to choose and configure a web server, e.g. gunicorn or uvicorn, and that eventually becomes a repetitive task.

Flama CLI precisely aims at reducing the effort invested in this step to the bare minimum, so that you can focus on developing your app.

Parameters

App path

To be able to serve an app, you need to have a file where you have the following lines:

from flama import Flama
app = Flama()
...

As we did in the Quickstart, let's assume that the file containing these lines is main.py, which is located at:

project_folder/
└── src
└── main.py
└── app

For Flama to be able to run the app, it needs to know the exact path to the app variable. The path can be passed directly to the CLI as argument, or by utilising the equivalent environment variable FLAMA_APP. Thus, if we are at project_folder (following the previous example of folder structure), we can run the app by either of the following two ways:

Using option argument

> flama run src.main:app
INFO: Started server process [3267]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Using environment variable

> export FLAMA_APP=src.main:app
> flama run
INFO: Started server process [3267]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Server parameters

Flama CLI uses uvicorn as webserver, and it is because of this that it accepts every argument of the uvicorn CLI:

> flama run --help
Usage: flama run [OPTIONS] FLAMA_APP
Run a Flama Application based on a route.
<FLAMA_APP> is the route to the Flama object to be served, e.g.
'examples.hello_flama:app'. This can be passed directly as argument of the
command line, or by environment variable.
Options:
--server-host TEXT Bind socket to this host. [default:
127.0.0.1]
--server-port INTEGER Bind socket to this port. [default: 8000]
--server-reload Enable auto-reload.
--server-uds TEXT Bind to a UNIX domain socket.
--server-fd INTEGER Bind to socket from this file descriptor.
--server-reload-dirs PATH Set reload directories explicitly, instead
of using the current working directory.
--server-reload-includes TEXT Set glob patterns to include while watching
for files. Includes '*.py' by default; these
defaults can be overridden with `--server-
reload-exclude`. This option has no effect
unless watchfiles is installed.
--server-reload-excludes TEXT Set glob patterns to exclude while watching
for files. Includes '.*, .py[cod], .sw.*,
~*' by default; these defaults can be
overridden with `--server-reload-include`.
This option has no effect unless watchfiles
is installed.
--server-reload-delay FLOAT Delay between previous and next check if
application needs to be. Defaults to 0.25s.
[default: 0.25]
--server-workers INTEGER Number of worker processes. Defaults to the
$WEB_CONCURRENCY environment variable if
available, or 1. Not valid with --server-
dev.
--server-loop [auto|asyncio|uvloop]
Event loop implementation. [default: auto]
--server-http [auto|h11|httptools]
HTTP protocol implementation. [default:
auto]
--server-ws [auto|none|websockets|wsproto]
WebSocket protocol implementation.
[default: auto]
--server-ws-max-size INTEGER WebSocket max size message in bytes
[default: 16777216]
--server-ws-ping-interval FLOAT
WebSocket ping interval [default: 20.0]
--server-ws-ping-timeout FLOAT WebSocket ping timeout [default: 20.0]
--server-ws-per-message-deflate BOOLEAN
WebSocket per-message-deflate compression
[default: True]
--server-lifespan [auto|on|off]
Lifespan implementation. [default: auto]
--server-interface [auto|asgi3|asgi2|wsgi]
Select ASGI3, ASGI2, or WSGI as the
application interface. [default: auto]
--server-env-file PATH Environment configuration file.
--server-log-config PATH Logging configuration file. Supported
formats: .ini, .json, .yaml.
--server-log-level [critical|error|warning|info|debug|trace]
Log level. [default: info]
--server-access-log / --server-no-access-log
Enable/Disable access log.
--server-use-colors / --server-no-use-colors
Enable/Disable colorized logging.
--server-proxy-headers / --server-no-proxy-headers
Enable/Disable X-Forwarded-Proto,
X-Forwarded-For, X-Forwarded-Port to
populate remote address info.
--server-server-header / --server-no-server-header
Enable/Disable default Server header.
--server-date-header / --server-no-date-header
Enable/Disable default Date header.
--server-forwarded-allow-ips TEXT
Comma separated list of IPs to trust with
proxy headers. Defaults to the
$FORWARDED_ALLOW_IPS environment variable if
available, or '127.0.0.1'.
--server-root-path TEXT Set the ASGI 'root_path' for applications
submounted below a given URL path.
--server-limit-concurrency INTEGER
Maximum number of concurrent connections or
tasks to allow, before issuing HTTP 503
responses.
--server-backlog INTEGER Maximum number of connections to hold in
backlog
--server-limit-max-requests INTEGER
Maximum number of requests to service before
terminating the process.
--server-timeout-keep-alive INTEGER
Close Keep-Alive connections if no new data
is received within this timeout. [default:
5]
--server-ssl-keyfile TEXT SSL key file
--server-ssl-certfile TEXT SSL certificate file
--server-ssl-keyfile-password TEXT
SSL keyfile password
--server-ssl-version INTEGER SSL version to use (see stdlib ssl module's)
[default: 17]
--server-ssl-cert-reqs INTEGER Whether client certificate is required (see
stdlib ssl module's) [default: 0]
--server-ssl-ca-certs TEXT CA certificates file
--server-ssl-ciphers TEXT Ciphers to use (see stdlib ssl module's)
[default: TLSv1]
--server-headers TEXT Specify custom default HTTP response headers
as a Name:Value pair
--server-app-dir TEXT Look for APP in the specified directory, by
adding this to the PYTHONPATH. Defaults to
the current working directory. [default: .]
--server-h11-max-incomplete-event-size INTEGER
For h11, the maximum number of bytes to
buffer of an incomplete event.
--server-factory Treat APP as an application factory, i.e. a
() -> <ASGI app> callable.
--help Show this message and exit.

For the exhaustive list of arguments you can pass, and their usage, we recommend you to check here.