Skip to content

Commit fe1308f

Browse files
committed
Fix Genie API code snippet
1 parent a766174 commit fe1308f

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

streamlit/views/genie_api.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import streamlit as st
1+
from typing import Dict
2+
3+
import pandas as pd
24
from databricks.sdk import WorkspaceClient
3-
from databricks.sdk.errors.sdk import OperationFailed
45
from databricks.sdk.service.dashboards import GenieMessage
5-
import pandas as pd
6-
from typing import Dict
76

7+
import streamlit as st
88

99
w = WorkspaceClient()
1010

@@ -16,23 +16,24 @@
1616
to let users ask questions about your data for instant insights.
1717
"""
1818
)
19-
st.warning("Public Preview")
2019

2120
tab_a, tab_b, tab_c = st.tabs(["**Try it**", "**Code snippet**", "**Requirements**"])
2221

2322
with tab_a:
23+
2424
def reset_conversation():
2525
st.session_state.conversation_id = None
2626
st.session_state.messages = []
2727

2828
genie_space_id = st.text_input(
29-
"Genie Space ID", placeholder="01efe16a65e21836acefb797ae6a8fe4", help="Room ID in the Genie Space URL"
29+
"Genie Space ID",
30+
placeholder="01efe16a65e21836acefb797ae6a8fe4",
31+
help="Room ID in the Genie Space URL",
3032
)
3133
if genie_space_id != st.session_state.get("genie_space_id", ""):
3234
reset_conversation()
3335
st.session_state.genie_space_id = genie_space_id
3436

35-
3637
def display_message(message: Dict):
3738
if "content" in message:
3839
st.markdown(message["content"])
@@ -42,19 +43,21 @@ def display_message(message: Dict):
4243
with st.expander("Show generated code"):
4344
st.code(message["code"], language="sql", wrap_lines=True)
4445

45-
46-
def get_query_result(statement_id: str) -> pd.DataFrame:
46+
def get_query_result(statement_id: str) -> pd.DataFrame:
4747
query = w.statement_execution.get_statement(statement_id)
4848
result = query.result.data_array
4949

5050
next_chunk = query.result.next_chunk_index
5151
while next_chunk:
52-
chunk = w.statement_execution.get_statement_result_chunk_n(statement_id, next_chunk)
52+
chunk = w.statement_execution.get_statement_result_chunk_n(
53+
statement_id, next_chunk
54+
)
5355
result.append(chunk.data_array)
5456
next_chunk = chunk.next_chunk_index
5557

56-
return pd.DataFrame(result, columns=[i.name for i in query.manifest.schema.columns])
57-
58+
return pd.DataFrame(
59+
result, columns=[i.name for i in query.manifest.schema.columns]
60+
)
5861

5962
def process_genie_response(response: GenieMessage):
6063
st.session_state.conversation_id = response.conversation_id
@@ -67,12 +70,14 @@ def process_genie_response(response: GenieMessage):
6770
elif i.query:
6871
data = get_query_result(i.query.statement_id)
6972
message = {
70-
"role": "assistant", "content": i.query.description, "data": data, "code": i.query.query
73+
"role": "assistant",
74+
"content": i.query.description,
75+
"data": data,
76+
"code": i.query.query,
7177
}
7278
display_message(message)
7379
st.session_state.messages.append(message)
7480

75-
7681
if "messages" not in st.session_state:
7782
st.session_state.messages = []
7883

@@ -93,25 +98,36 @@ def process_genie_response(response: GenieMessage):
9398
genie_space_id, st.session_state.conversation_id, prompt
9499
)
95100
except Exception as e:
96-
status.update(label="Conversation failed. Check the required permissions.", state="error")
101+
status.update(
102+
label="Conversation failed. Check the required permissions.",
103+
state="error",
104+
)
97105
st.button("New Chat", on_click=reset_conversation)
98106
raise e
99107
if conversation.error:
100108
st.error(conversation.error.type, conversation.error.error)
101109
process_genie_response(conversation)
102110
else:
103111
try:
104-
conversation = w.genie.start_conversation_and_wait(genie_space_id, prompt)
112+
conversation = w.genie.start_conversation_and_wait(
113+
genie_space_id, prompt
114+
)
105115
except Exception as e:
106-
status.update(label="Failed to initialize Genie. Check the required permissions.", state="error")
116+
status.update(
117+
label="Failed to initialize Genie. Check the required permissions.",
118+
state="error",
119+
)
107120
st.button("New Chat", on_click=reset_conversation)
108121
raise e
109122
if conversation.error:
110123
st.error(conversation.error.type, conversation.error.error)
111124
process_genie_response(conversation)
112125
status.update(label="", state="complete")
113126
st.button("New Chat", on_click=reset_conversation)
114-
st.link_button("Open Genie", f"{w.config.host}/genie/rooms/{genie_space_id}/chats/{st.session_state.conversation_id}")
127+
st.link_button(
128+
"Open Genie",
129+
f"{w.config.host}/genie/rooms/{genie_space_id}/chats/{st.session_state.conversation_id}",
130+
)
115131
else:
116132
st.error("Please fill in the Genie Space ID.")
117133

@@ -154,7 +170,7 @@ def process_genie_response(response):
154170
message = {"role": "assistant", "content": i.text.content}
155171
display_message(message)
156172
elif i.query:
157-
data = get_query_result(response.query_result.statement_id)
173+
data = get_query_result(i.query.statement_id)
158174
message = {
159175
"role": "assistant", "content": i.query.description, "data": data, "code": i.query.query
160176
}
@@ -188,15 +204,15 @@ def process_genie_response(response):
188204
* `CAN VIEW` the Genie Space
189205
"""
190206
)
191-
207+
192208
with col2:
193209
st.markdown(
194210
"""
195211
**Databricks resources**
196212
* Genie API
197213
"""
198214
)
199-
215+
200216
with col3:
201217
st.markdown(
202218
"""

0 commit comments

Comments
 (0)