Skip to content

Commit 10b945b

Browse files
authored
Run Command from App Comments (#15577)
* initial work * this seems to work well * added example test * updated docs & logging * fixed errors * fix typing error * now using the --setup flag to decide if we should execute app comment commands or not * updated tests * added tests * added test to ci * fixed failing tests * code review * updates
1 parent f4ca562 commit 10b945b

28 files changed

+502
-10
lines changed

.azure/app-cloud-e2e.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ jobs:
6767
name: "collect_failures"
6868
'App: custom_work_dependencies':
6969
name: "custom_work_dependencies"
70+
'App: installation_commands_app':
71+
name: "installation_commands_app"
7072
'App: drive':
7173
name: "drive"
7274
'App: payload':

docs/source-app/levels/basic/hello_components/build_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# pip install streamlit omegaconf scipy
2-
# pip install torch
31
# app.py
2+
# !pip install streamlit omegaconf scipy
3+
# !pip install torch
44

55
import lightning as L
66
import torch

docs/source-app/levels/basic/hello_components/multi_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ! pip install torch
1+
# !pip install torch
22
import lightning as L
33
from lightning.app.components import MultiNode
44

docs/source-app/levels/basic/hello_components/pt_multinode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ! pip install torch
1+
# !pip install torch
22
import lightning as L
33
from lightning.app.components import MultiNode
44
import torch

docs/source-app/levels/basic/hello_components/xgboost.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# pip install sklearn xgboost
21
# app.py
2+
# !pip install sklearn xgboost
33

44
import lightning as L
55
from sklearn import datasets

docs/source-app/levels/basic/hello_components/xgboost_gpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# pip install sklearn xgboost
2-
# conda install py-xgboost-gpu
31
# app.py
2+
# !pip install sklearn xgboost
3+
# !conda install py-xgboost-gpu
44
import lightning as L
55
from sklearn import datasets
66
from sklearn.model_selection import train_test_split
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# EXAMPLE COMPONENT: RUN A SCRIPT
2+
# app.py
3+
# !echo "I am installing a dependency not declared in a requirements file"
4+
# !pip install lmdb
5+
import lmdb
6+
7+
import lightning as L
8+
9+
10+
class YourComponent(L.LightningWork):
11+
def run(self):
12+
print(lmdb.__version__)
13+
print("lmdb successfully installed")
14+
15+
16+
# run on a cloud machine
17+
compute = L.CloudCompute("cpu")
18+
worker = YourComponent(cloud_compute=compute)
19+
app = L.LightningApp(worker)

src/lightning_app/cli/lightning_cli.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def _run_app(
239239
open_ui: bool,
240240
env: tuple,
241241
secret: tuple,
242+
run_app_comment_commands: bool,
242243
) -> None:
243244
file = _prepare_file(file)
244245

@@ -285,6 +286,7 @@ def on_before_run(*args: Any, **kwargs: Any) -> None:
285286
env_vars=env_vars,
286287
secrets=secrets,
287288
cluster_id=cluster_id,
289+
run_app_comment_commands=run_app_comment_commands,
288290
)
289291
if runtime_type == RuntimeType.CLOUD:
290292
click.echo("Application is ready in the cloud")
@@ -322,6 +324,14 @@ def run() -> None:
322324
@click.option("--env", type=str, default=[], multiple=True, help="Environment variables to be set for the app.")
323325
@click.option("--secret", type=str, default=[], multiple=True, help="Secret variables to be set for the app.")
324326
@click.option("--app_args", type=str, default=[], multiple=True, help="Collection of arguments for the app.")
327+
@click.option(
328+
"--setup",
329+
"-s",
330+
"run_app_comment_commands",
331+
is_flag=True,
332+
default=False,
333+
help="run environment setup commands from the app comments.",
334+
)
325335
def run_app(
326336
file: str,
327337
cloud: bool,
@@ -334,9 +344,22 @@ def run_app(
334344
env: tuple,
335345
secret: tuple,
336346
app_args: tuple,
347+
run_app_comment_commands: bool,
337348
) -> None:
338349
"""Run an app from a file."""
339-
_run_app(file, cloud, cluster_id, without_server, no_cache, name, blocking, open_ui, env, secret)
350+
_run_app(
351+
file,
352+
cloud,
353+
cluster_id,
354+
without_server,
355+
no_cache,
356+
name,
357+
blocking,
358+
open_ui,
359+
env,
360+
secret,
361+
run_app_comment_commands,
362+
)
340363

341364

342365
@_main.group(hidden=True)

src/lightning_app/core/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def get_lightning_cloud_url() -> str:
5959
int(os.getenv("ENABLE_MULTIPLE_WORKS_IN_NON_DEFAULT_CONTAINER", "0"))
6060
) # This isn't used in the cloud yet.
6161

62+
# env var trigger running setup commands in the app
63+
ENABLE_APP_COMMENT_COMMAND_EXECUTION = bool(int(os.getenv("ENABLE_APP_COMMENT_COMMAND_EXECUTION", "0")))
64+
6265

6366
DEBUG: bool = lightning_cloud.env.DEBUG
6467
DEBUG_ENABLED = bool(int(os.getenv("LIGHTNING_DEBUG", "0")))

src/lightning_app/runners/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
from lightning_app.runners.multiprocess import MultiProcessRuntime
33
from lightning_app.runners.runtime import dispatch, Runtime
44
from lightning_app.runners.singleprocess import SingleProcessRuntime
5+
from lightning_app.utilities.app_commands import run_app_commands
56
from lightning_app.utilities.load_app import load_app_from_file
67

7-
__all__ = ["dispatch", "load_app_from_file", "Runtime", "MultiProcessRuntime", "SingleProcessRuntime", "CloudRuntime"]
8+
__all__ = [
9+
"dispatch",
10+
"load_app_from_file",
11+
"run_app_commands",
12+
"Runtime",
13+
"MultiProcessRuntime",
14+
"SingleProcessRuntime",
15+
"CloudRuntime",
16+
]

0 commit comments

Comments
 (0)