Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Helps fix #26 by making them optional dependencies
Browse files Browse the repository at this point in the history
To ensure a delightful user experience, this commit adds
to `extras_require` for graphviz and networkx. This will mean
that users will have to do pip install sf-hamilton[visualization]
to be able to get a DAG visualized.

Rather than doing a major version bump, we're doing a minor one;
this shouldn't break any body's core use, since they shouldn't be
visualizing the DAG in production each time.

Otherwise this updates the docs with this information.

Adjusts behavior when graphivz or networkx aren't present

Rather than causing an execute to fail, we'll instead log the
error and return early.

The message to the user should look something like:

```
ERROR:hamilton.graph: graphviz is required for visualizing the function graph. Install it with:

  pip install sf-hamilton[visualization] or pip install graphviz

Traceback (most recent call last):
  File "/Users/stefankrawczyk/temp/hamilton/hamilton/graph.py", line 142, in display
    import graphviz
ModuleNotFoundError: No module named 'graphviz'
```
  • Loading branch information
skrawcz committed Dec 12, 2021
1 parent f00dbf4 commit 006523a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Requirements:
To get started, first you need to install hamilton. It is published to pypi under `sf-hamilton`:
> pip install sf-hamilton
Note: to use the DAG visualization functionality, you should instead do:
> pip install sf-hamilton[visualization]
While it is installing we encourage you to start on the next section.

Note: the content (i.e. names, function bodies) of our example code snippets are for illustrative purposes only, and don't reflect what we actually do internally.
Expand Down Expand Up @@ -92,14 +95,16 @@ output_columns = [
'spend_per_signup',
]
# let's create the dataframe!
df = dr.execute(output_columns, display_graph=True)
# if you only did `pip install sf-hamilton` earlier:
df = dr.execute(output_columns)
# else if you did `pip install sf-hamilton[visualization]` earlier:
# df = dr.execute(output_columns, display_graph=True)
print(df)
```
3. Run my_script.py
> python my_script.py
You should see the following output:
![hello_world_image](hello_world_image.png)

spend signups avg_3wk_spend spend_per_signup
0 10 1 NaN 10.000
Expand All @@ -109,6 +114,10 @@ You should see the following output:
4 40 200 33.333333 0.200
5 50 400 43.333333 0.125

You should see the following image if you set `display_graph=True`:

![hello_world_image](hello_world_image.png)

Congratulations - you just created your first dataframe with Hamilton!

# License
Expand Down
1 change: 1 addition & 0 deletions examples/hello_world/my_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
]
# let's create the dataframe!
df = dr.execute(output_columns, display_graph=True)
# do `pip install sf-hamilton[visualization]` if you want display_graph=True to work.
print(df)
16 changes: 9 additions & 7 deletions hamilton/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,23 @@ def display(nodes: Set[node.Node], user_nodes: Set[node.Node], output_file_path:
:param final_vars: the final vars we want -- else all if None.
:param output_file_path: the path where we want to store the a `dot` file + pdf picture.
"""
# Check to see if optional dependencies have been installed.
try:
import graphviz
except ModuleNotFoundError:
raise ModuleNotFoundError(
'graphviz is required for visualizing the function graph. Install it with:'
'\n\n pip install graphviz'
logger.exception(
' graphviz is required for visualizing the function graph. Install it with:'
'\n\n pip install sf-hamilton[visualization] or pip install graphviz \n\n'
)

return
try:
import networkx
except ModuleNotFoundError:
raise ModuleNotFoundError(
'networkx is required for visualizing the function graph. Install it with:'
'\n\n pip install networkx'
logger.exception(
' networkx is required for visualizing the function graph. Install it with:'
'\n\n pip install sf-hamilton[visualization] or pip install networkx \n\n'
)
return

dot = graphviz.Digraph(comment='Dependency Graph')

Expand Down
2 changes: 1 addition & 1 deletion hamilton/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = (1, 1, 1)
VERSION = (1, 2, 0)
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def load_test_requirements():
# default version installed with virtualenv. Make sure to update your tools!
python_requires='>=3.6, <4',

# adding this to slim the package down, since these dependencies are only used to visualize the DAG.
extras_require={'visualization': ['graphviz', 'networkx']},

# Relevant project URLs
project_urls={ # Optional
'Bug Reports': 'https://github.com/stitchfix/hamilton/issues',
Expand Down

0 comments on commit 006523a

Please sign in to comment.