Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[App] Introduce Commands #13602

Merged
merged 79 commits into from
Jul 25, 2022
Merged

[App] Introduce Commands #13602

merged 79 commits into from
Jul 25, 2022

Conversation

tchaton
Copy link
Contributor

@tchaton tchaton commented Jul 11, 2022

What does this PR do?

This PR introduces Lightning App Commands.

Commands can be configured from the app by overriding the configure_commands hook on the flow and returning a list of dictionaries mapping a command_name to a flow method.

Once the app is running, someone can remotely execute those methods from the CLI by providing the command_name.

Here is how it looks:

from lightning import LightningFlow
from lightning_app.core.app import LightningApp


class ChildFlow(LightningFlow):
    def trigger_method(self, name: str):
        print(name)

    def configure_commands(self):
        return [{"nested_trigger_command": self.trigger_method}]


class FlowCommands(LightningFlow):
    def __init__(self):
        super().__init__()
        self.names = []
        self.child_flow = ChildFlow()

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

    def trigger_method(self, name: str):
        self.names.append(name)

    def configure_commands(self):
        return [{"flow_trigger_command": self.trigger_method}] + self.child_flow.configure_commands()


app = LightningApp(FlowCommands())

Once the app is running with

lightning run app app.py

Trigger one of its commands as follows

lightning run command flow_trigger_command --args name=my_name

To make it work in the cloud, add id.

lightning run command flow_trigger_command --args name=my_name --id=01g7q3kg9kvtsdx4ak0cdmrbe8

Fixed #13637

Alternatives API:

  • lightning run function
  • lightning run task
  • lightning exec ...
  • lightning execute

Does your PR introduce any breaking changes? If yes, please list them.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

cc @Borda

@tchaton tchaton added app (removed) Generic label for Lightning App package lightningflow labels Jul 11, 2022
@tchaton tchaton changed the title Introduce Lightning Commands [App] Introduce Commands Jul 11, 2022
@tchaton tchaton requested review from lantiga and manskx and removed request for SeanNaren July 22, 2022 22:54
Copy link
Contributor

@manskx manskx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@awaelchli awaelchli dismissed their stale review July 25, 2022 12:03

dismiss

@mergify mergify bot added the ready PRs ready to be merged label Jul 25, 2022
MANIFEST.in Outdated Show resolved Hide resolved
Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unblock

@tchaton tchaton enabled auto-merge (squash) July 25, 2022 16:56
@tchaton tchaton added this to the app:0.6 milestone Jul 25, 2022
@tchaton tchaton merged commit 4c35867 into master Jul 25, 2022
@tchaton tchaton deleted the lightning_commands branch July 25, 2022 17:13
@@ -155,7 +156,7 @@ jobs:
shell: bash
run: |
mkdir -p ${VIDEO_LOCATION}
HEADLESS=1 python -m pytest tests/tests_app_examples/test_${{ matrix.app_name }}.py::test_${{ matrix.app_name }}_example_cloud --timeout=900 --capture=no -v --color=yes
HEADLESS=1 PACKAGE_LIGHTNING=1 python -m pytest tests/tests_app_examples/test_${{ matrix.app_name }}.py::test_${{ matrix.app_name }}_example_cloud --timeout=900 --capture=no -v --color=yes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, are this env. var described somewhere?

while not self._exit_event.is_set():
self.run_once()
try:
while not self._exit_event.is_set():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this accidentally turn into an infinite loop?

data = await request.json()
command_name = data.get("command_name", None)
if not command_name:
raise Exception("The provided command name is empty.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tchaton / @manskx Can we add as a follow-up also exception types?



def makedirs(path: str):
r"""Recursive directory creation function."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not use r in these simple strings :)

Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tchaton, mind adding some more docstrings to newly added classes? 🐰

@manskx manskx restored the lightning_commands branch August 17, 2022 21:19
manskx added a commit that referenced this pull request Aug 17, 2022
@manskx manskx deleted the lightning_commands branch August 22, 2022 11:20
Borda pushed a commit that referenced this pull request Aug 22, 2022
lantiga pushed a commit that referenced this pull request Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app (removed) Generic label for Lightning App package ready PRs ready to be merged
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add an RPC mechanism to Lightning App
8 participants