Shortcuts

PopenPythonScript

class lightning_app.components.python.popen.PopenPythonScript(script_path, script_args=None, env=None, **kwargs)[source]

Bases: lightning_app.core.work.LightningWork

The PopenPythonScript component enables to easily run a python script within a subprocess.

Parameters
  • script_path (Union[str, Path]) – Path of the python script to run.

  • script_path – The arguments to be passed to the script.

  • env (Optional[Dict]) – Environment variables to be passed to the script.

  • kwargs – LightningWork keyword arguments.

Raises

FileNotFoundError – If the provided script_path doesn’t exists.

Example

>>> from lightning_app.components.python import PopenPythonScript
>>> f = open("a.py", "w")
>>> f.write("print('Hello World !')")
22
>>> f.close()
>>> python_script = PopenPythonScript("a.py")
>>> python_script.run()
>>> os.remove("a.py")

In this example, the script will be launch with the Popen.

from pathlib import Path

from lightning.app.components import PopenPythonScript

if __name__ == "__main__":
    comp = PopenPythonScript(Path(__file__).parent / "pl_script.py")
    comp.run()
configure_tracer()[source]

Override this hook to customize your tracer when running PythonScript with mode=tracer.

Return type

Tracer

on_after_run()[source]

Called after the python script is executed.

on_before_run()[source]

Called before the python script is executed.

on_exit()[source]

Override this hook to add your logic when the work is exiting.

run()[source]

Override to add your own logic.

Raises

LightningPlatformException – If resource exceeds platform quotas or other constraints.

Return type

None