Basic Usage#
Have uv? β‘
If you have uv installed, you can instantly open this page as a Jupyter notebook using opennb:
uvx --with "pipefunc[docs]" opennb pipefunc/pipefunc/docs/source/examples/basic-usage.md
This command creates an ephemeral environment with all dependencies and launches the notebook in your browser in 1 second - no manual setup needed! β¨.
Alternatively, run:
uv run https://raw.githubusercontent.com/pipefunc/pipefunc/refs/heads/main/get-notebooks.py
to download all documentation as Jupyter notebooks.
This example demonstrates a simple pipeline using the @pipefunc decorator and the Pipeline class. It showcases a basic workflow with sequential execution.
Code#
from pipefunc import pipefunc, Pipeline
@pipefunc(output_name="c")
def f(a, b):
return a + b
@pipefunc(output_name="d")
def g(b, c, x=1):
return b * c * x
@pipefunc(output_name="e")
def h(c, d, x=1):
return c * d * x
pipeline = Pipeline([f, g, h])
pipeline.visualize()
pipeline # Display the pipeline
Pipeline Info ββββββββββββββββββββββββ¦ββββββββββ β Category β Items β β βββββββββββββββββββββββ¬ββββββββββ£ β required_inputs β a, b β β optional_inputs β x β β inputs β a, b, x β β intermediate_outputs β c, d β β outputs β e β ββββββββββββββββββββββββ©ββββββββββ
result = pipeline("e", a=2, b=3) # Or: pipeline.run("e", kwargs={"a": 2, "b": 3})
print(result)
75
Explanation#
Define βPipeableβ Functions: We define three functions,
f,g, andh. Each is decorated with@pipefunc, making it usable within aPipeline.output_nameassigns a name to each functionβs output.Create Pipeline: A
Pipelineobject is created usingPipeline([f, g, h]). The order of functions in this list does not affect execution order.Visualize Pipeline: The pipeline is visualized using
pipeline.visualize(). This shows the function dependencies.Execute Pipeline: The pipeline is run using
pipeline("e", a=2, b=3)."e"indicates that we want the output of functionh(which hasoutput_name="e").a=2, b=3provide input arguments.The pipeline automatically determines the correct execution order based on function dependencies.
Sequential Execution: In this example, the functions are executed sequentially based on their dependencies:
f(a=2, b=3)producesc=5.g(b=3, c=5, x=1)producesd=15.h(c=5, d=15, x=1)producese=75.
Features Demonstrated:
@pipefunc: Decorator to make a function βpipeable.βPipeline: Class to create and manage a pipeline of functions.Visualization using
visualize().Sequential execution using
pipeline()(or equivalently,run()).
Further Exploration:
For more details on creating pipelines, see the main tutorial.
Already familiar with the basics? Check out the Physics Based Example for an example using some awesome
pipefuncfeatures!