PanelFrontend

class lightning.app.frontend.panel.PanelFrontend(entry_point)[source]

Bases: Frontend

The PanelFrontend enables you to serve Panel code as a Frontend for your LightningFlow.

Reference: https://lightning.ai/lightning-docs/workflows/add_web_ui/panel/

Parameters:

entry_point (str | Callable) – The path to a .py or .ipynb file, or a pure function. The file or function must contain your Panel code. The function can optionally accept an AppStateWatcher argument.

Raises:

TypeError – Raised if the entry_point provided is a class method

Example:

To use the PanelFrontend, you must first install the panel package:

pip install panel

Create the files panel_app_basic.py and app_basic.py with the content below.

panel_app_basic.py

import panel as pn

pn.panel("Hello **Panel ⚡** World").servable()

app_basic.py

from lightning.app import LightningFlow, LightningApp
from lightning.app.frontend.panel import PanelFrontend


class LitPanel(LightningFlow):
    def configure_layout(self):
        return PanelFrontend("panel_app_basic.py")


class LitApp(LightningFlow):
    def __init__(self):
        super().__init__()
        self.lit_panel = LitPanel()

    def configure_layout(self):
        return {"name": "home", "content": self.lit_panel}


app = LightningApp(LitApp())

Start the Lightning server with lightning run app app_basic.py.

For development you can get Panel autoreload by setting the PANEL_AUTORELOAD environment variable to ‘yes’, i.e. run PANEL_AUTORELOAD=yes lightning run app app_basic.py

start_server(host, port, root_path='')[source]

Start the process that serves the UI at the given hostname and port number.

Parameters:
  • host (str) – The hostname where the UI will be served. This gets determined by the dispatcher (e.g., cloud), but defaults to localhost when running locally.

  • port (int) – The port number where the UI will be served. This gets determined by the dispatcher, which by default chooses any free port when running locally.

  • root_path (str) – root_path for the server if app in exposed via a proxy at /<root_path>

Return type:

None

Example

An custom implementation could look like this:

def start_server(self, host, port, root_path=""):
    self._process = subprocess.Popen(["flask", "run" "--host", host, "--port", str(port)])
stop_server()[source]

Stop the process that was started with start_server() so the App can shut down.

This method gets called when the LightningApp terminates.

Return type:

None

Example

def stop_server(self):
    self._process.kill()