Skip to content

Commit 7dd1890

Browse files
committed
Get Snowflake + Hamilton UI example ready
Note: this is a toy example. For real production needs, you'd need to modify a few things: 1. not use SQLLITE, and instead postgresql, or implement django-snowflake connection. 2. likely not use the Hamilton code within the flask container, instead package up properly and define a UDF or UDTF. 3. could use snowpark dataframes or have hamilton code do other things, e.g. LLM calls..
1 parent 7abc9d7 commit 7dd1890

File tree

6 files changed

+42
-140
lines changed

6 files changed

+42
-140
lines changed

docs/hamilton-ui/ui.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ More extensive self-hosting documentation is in the works, e.g. Snowflake, Datab
123123
chart contribution!
124124

125125

126+
Running on Snowflake
127+
---------------------
128+
You can run the Hamilton UI on Snowflake Container Services. For a detailed guide, see the blog post
129+
`Observability of Python code and application logic with Hamilton UI on Snowflake Container Services <https://medium.com/@pkantyka/observability-of-python-code-and-application-logic-with-hamilton-ui-on-snowflake-container-services-a26693b46635>`_ by
130+
`Greg Kantyka <https://medium.com/@pkantyka>`_ and the `Hamilton Snowflake Example <https://github.com/DAGWorks-Inc/hamilton/tree/main/examples/snowflake/hamilton_ui>`_.
131+
126132
-----------
127133
Get started
128134
-----------
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# Running the Hamilton & the Hamilton UI in Snowflake
22

3-
This example is code for the ["TODO" post]().
3+
This example is code for the ["Observability of Python code and application logic with Hamilton UI on Snowflake Container Services" post](https://medium.com/@pkantyka/observability-of-python-code-and-application-logic-with-hamilton-ui-on-snowflake-container-services-a26693b46635) by
4+
[Greg Kantyka](https://medium.com/@pkantyka).
45

5-
Here we show the code required to be packaged up for
6-
use on Snowflake.
6+
Here we show the code required to be packaged up for use on Snowflake:
77

8-
TODO:
9-
- cut & paste instructions from blog post?
8+
1. Docker file that runs the Hamilton UI and a flask endpoint to exercise Hamilton code
9+
2. my_functions.py - the Hamilton code that is exercised by the flask endpoint
10+
3. pipeline_endpoint.py - the flask endpoint that exercises the Hamilton code
11+
12+
To run see:
13+
- snowflake.sql that contains all the SQL to create the necessary objects in Snowflake and exercise things.
14+
15+
For more details see ["Observability of Python code and application logic with Hamilton UI on Snowflake Container Services" post](https://medium.com/@pkantyka/observability-of-python-code-and-application-logic-with-hamilton-ui-on-snowflake-container-services-a26693b46635) by
16+
[Greg Kantyka](https://medium.com/@pkantyka).

examples/snowflake/hamilton_ui/pipeline_endpoint.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
"""
2+
This module:
3+
- Defines a flask app that listens for POST requests on /echo
4+
- the /echo command is invoked from a Snowflake SQL query
5+
- the /echo command calls a function get_response that is defined in this module
6+
- get_response uses Hamilton to execute a pipeline defined in my_functions.py
7+
- my_functions.py contains the functions that are used in the pipeline
8+
- the pipeline is executed with the input data from the Snowflake query
9+
- the output of the pipeline is returned to Snowflake
10+
- the Hamilton UI tracker is used to track the execution of the pipeline
11+
"""
12+
113
import logging
214
import os
315
import sys
@@ -13,7 +25,7 @@
1325
from hamilton import driver
1426
from hamilton_sdk import adapters
1527

16-
# WRAPER CODE FOR SNOWFLAKE FUNCTION ######
28+
# WRAPPER CODE FOR SNOWFLAKE FUNCTION ######
1729

1830
SERVICE_HOST = os.getenv("SERVER_HOST", "0.0.0.0")
1931
SERVICE_PORT = os.getenv("SERVER_PORT", 8080)
@@ -42,6 +54,7 @@ def readiness_probe():
4254

4355
@app.post("/echo")
4456
def echo():
57+
"""This is the endpoint that Snowflake will call to run Hamilton code."""
4558
message = request.json
4659
logger.debug(f"Received request: {message}")
4760

@@ -61,17 +74,18 @@ def echo():
6174
return response
6275

6376

64-
# END OF WRAPER CODE FOR SNOWFLAKE FUNCTION ######
77+
# END OF WRAPPER CODE FOR SNOWFLAKE FUNCTION ######
6578

6679

6780
def get_response(prj_id, spend, signups, output_columns):
81+
"""The function that is called from SQL on Snowflake."""
6882
tracker = adapters.HamiltonTracker(
6983
project_id=prj_id,
7084
username="admin",
7185
dag_name="MYDAG",
7286
tags={"environment": "R&D", "team": "MY_TEAM", "version": "Beta"},
7387
)
74-
initial_columns = { # load from actuals or wherever -- this is our initial data we use as input.
88+
input_columns = {
7589
"signups": pd.Series(spend),
7690
"spend": pd.Series(signups),
7791
}
@@ -81,11 +95,11 @@ def get_response(prj_id, spend, signups, output_columns):
8195
.with_modules(
8296
my_functions
8397
) # we need to tell hamilton where to load function definitions from
84-
.with_adapters(tracker) # we want a pandas dataframe as output
98+
.with_adapters(tracker) # we add the Hamilton UI tracker
8599
.build()
86100
)
87101

88-
df = dr.execute(output_columns, inputs=initial_columns)
102+
df = dr.execute(output_columns, inputs=input_columns)
89103

90104
serializable_df = {}
91105

examples/snowflake/hamilton_ui/pipeline_endpoint_.py

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flask
2+
sf-hamilton[ui,sdk]

examples/snowflake/hamilton_ui/snowflake.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
https://medium.com/@pkantyka/observability-of-python-code-and-application-logic-with-hamilton-ui-on-snowflake-container-services-a26693b46635
1+
-- For more details visit:
2+
-- https://medium.com/@pkantyka/observability-of-python-code-and-application-logic-with-hamilton-ui-on-snowflake-container-services-a26693b46635
23

34
CREATE SERVICE public.hamilton_ui
45
IN COMPUTE POOL TEST_POOL
@@ -90,4 +91,4 @@ FROM
9091
flattened f
9192
left join lateral flatten(metric_value) f2
9293
where
93-
metric_key = 'spend_zero_mean_unit_variance';
94+
metric_key = 'spend_zero_mean_unit_variance';

0 commit comments

Comments
 (0)