1. Develop a CLI with server side code only

We are going to learn how to create a simple command-line interface.

Lightning provides a flexible way to create complex CLI without much effort.


1. Implement a simple CLI

To create your first CLI, you need to override the configure_commands hook and return a list of dictionaries where the keys are the commands and the values are the server side handlers.

First, create a file app.py and copy-paste the following code in to the file:

from lightning import LightningApp, LightningFlow


class Flow(LightningFlow):
    def __init__(self):
        super().__init__()
        self.names = []

    def run(self):
        print(self.names)

    def add_name(self, name: str):
        """Add a name."""
        print(f"Received name: {name}")
        self.names.append(name)

    def configure_commands(self):
        # This can be invoked with `lightning add --name=my_name`
        commands = [
            {"add": self.add_name},
        ]
        return commands


app = LightningApp(Flow())

2. Run the App

Execute the following command in a terminal:

lightning_app run app app.py

The following appears the terminal:

Your Lightning App is starting. This won't take long.
INFO: Your app has started. View it in your browser: http://127.0.0.1:7501/view
[]

3. Connect to a running App

In another terminal, connect to the running App. When you connect to an App, the Lightning CLI is replaced by the App CLI. To exit the App CLI, you need to run lightning_app disconnect.

lightning_app connect localhost

To see a list of available commands:

lightning_app --help
You are connected to the cloud Lightning App: localhost.
Usage: lightning_app [OPTIONS] COMMAND [ARGS]...

--help     Show this message and exit.

Lightning App Commands
    add Add a name.

To find the arguments of the commands:

lightning_app add --help
You are connected to the cloud Lightning App: localhost.
Usage: lightning_app add [ARGS]...

Options
    name: Add description

4. Execute a command

Trigger the command line exposed by your App:

lightning_app add --name=my_name
WARNING: Lightning Command Line Interface is an experimental feature and unannounced changes are likely.

In your first terminal, Received name: my_name and [“my_name”] are printed.

Your Lightning App is starting. This won't take long.
INFO: Your app has started. View it in your browser: http://127.0.0.1:7501/view
[]
Received name: my_name
["my_name]

5. Disconnect from the App

To exit the App CLI, you need to run lightning_app disconnect.

lightning_app disconnect
You are disconnected from the local Lightning App.