Hello World
Let’s start with a very simple example: a Bionic flow that generates the text “Hello world!”
(The code for this example is available in the Bionic repo at example/hello_world.py.)
1import bionic as bn
2
3# Initialize the builder object we'll use to construct our flow.
4builder = bn.FlowBuilder("hello_world")
5
6# Define new entities "greeting" and "subject" with fixed values.
7builder.assign("greeting", "Hello")
8builder.assign("subject", "world")
9
10
11# Define a "message" entity, constructed by taking the values of "greeting" and
12# "subject" and combining them in a sentence.
13# The `@builder` decorator tells Bionic to define a new derived entity; Bionic
14# infers the name of the new entity ("message") and the names of its
15# dependencies ("greeting" and "subject").
16@builder
17def message(greeting, subject):
18 return f"{greeting} {subject}!"
19
20
21# Assemble the flow object, which is capable of computing any of the entities
22# we've defined.
23flow = builder.build()
24
25if __name__ == "__main__":
26 # Use our flow to compute the message "Hello world!"
27 print(flow.get("message"))
We can run this code (assuming we’ve checked out the bionic
repo) like
this:
> cd bionic
> python example/hello_world.py
Hello world!
We can also import it in an interpreter or notebook:
[1]:
# Configure the PYTHONPATH for this notebook.
import _tutorial_setup
from example.hello_world import flow
flow.get('message')
[1]:
'Hello world!'
Although our flow
object is immutable, we can easily make a new copy with a different value of subject
:
[2]:
new_flow = flow.setting('subject', 'universe')
new_flow.get('message')
[2]:
'Hello universe!'
We can also try changing the message
directly:
[3]:
flow.setting('message', 'Goodbye world!').get('message')
[3]:
'Goodbye world!'
As a convenience, setting
and get
can be called by an alternative syntax which makes it easier for your notebook or interpreter to autocomplete entity names:
[4]:
flow.setting.subject('universe').get.message()
[4]:
'Hello universe!'
Finally, we can visualize our flow as a directed acyclic graph:
[5]:
flow.render_dag()
[5]:
This flow doesn’t do much, but it illustrates how flows can be constructed, used, and modified. The next tutorial will demonstrate a more practical example.