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.)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import bionic as bn

# Initialize the builder object we'll use to construct our flow.
builder = bn.FlowBuilder('hello_world')

# Define new entities "greeting" and "subject" with fixed values.
builder.assign('greeting', 'Hello')
builder.assign('subject', 'world')


# Define a "message" entity, constructed by taking the values of "greeting" and
# "subject" and combining them in a sentence.
# The `@builder` decorator tells Bionic to define a new derived entity; Bionic
# infers the name of the new entity ("message") and the names of its
# dependencies ("greeting" and "subject").
@builder
def message(greeting, subject):
    return '{0} {1}!'.format(greeting, subject)


# Assemble the flow object, which is capable of computing any of the entities
# we've defined.
flow = builder.build()

if __name__ == '__main__':
    # Use our flow to compute the message "Hello world!"
    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]:
../_images/tutorials_hello_world_10_0.png

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.