Alex Ilyin
01/31/2025, 10:02 PMMarvin
01/31/2025, 10:02 PM@flow
decorator to define your flow in a Python script. Make sure to include logging using get_run_logger()
for capturing logs.
python
from prefect import flow, get_run_logger
@flow
def my_flow():
logger = get_run_logger()
<http://logger.info|logger.info>("This is a log message from my flow.")
2. Run the flow from the command line: You can execute the script directly using Python. Make sure your Prefect environment is configured correctly (e.g., Prefect server or Prefect Cloud is set up).
bash
python my_flow_script.py
3. View logs: The logs will appear in the command line where you ran the script. The logging mechanism in Prefect ensures that logs are captured and can also be viewed in the Prefect UI if configured to a backend.
For more detailed guidance, you can refer to the Prefect 2.x documentation on running flows and logging.Alex Ilyin
01/31/2025, 10:08 PMMarvin
01/31/2025, 10:08 PMprefect deployment run
command with the --params
option. This allows you to specify parameters as a JSON string. Here's how you can do it:
1. Define your flow with parameters:
python
from prefect import flow
@flow
def my_flow(param1: int, param2: str):
print(f"Parameter 1: {param1}, Parameter 2: {param2}")
2. Run your flow with parameters:
You can pass parameters using the --params
flag in your command line execution:
bash
prefect deployment run 'my-flow-deployment' --params '{"param1": 42, "param2": "Hello"}'
This command will execute the flow with the specified parameters.
For more details, you can refer to the Prefect 2.x documentation on deployments and parameters.Nate
01/31/2025, 10:23 PMMarvin
01/31/2025, 10:23 PMsys.argv
to parse command-line arguments directly in your script. Here's a simple example:
1. Modify your flow to accept parameters from sys.argv
:
python
import sys
from prefect import flow
@flow
def my_flow(param1: int, param2: str):
print(f"Parameter 1: {param1}, Parameter 2: {param2}")
if __name__ == "__main__":
param1 = int(sys.argv[1])
param2 = sys.argv[2]
my_flow(param1, param2)
2. Run your script with command-line arguments:
bash
python my_flow_script.py 42 "Hello"
This approach leverages Python's standard library to handle command-line arguments directly, which can be more straightforward for local execution without Prefect's orchestration layer.