Skip to content

Commit 1804376

Browse files
committed
feat(pkg-py): Add .app() method, enable bookmarking by default
1 parent aab8b0a commit 1804376

File tree

6 files changed

+161
-24
lines changed

6 files changed

+161
-24
lines changed

pkg-py/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [UNRELEASED]
9+
10+
### New features
11+
12+
* New `QueryChat.app()` method enables quicker/easier chatting with a dataset. (#xx)
13+
* Enabled bookmarking by default in both `.app()` and `.server()` methods. In latter case, you'll need to also specify the `bookmark_store` (either in `shiny.App()` or `shiny.express.app_opts()`) for it to take effect. (#xx)
14+
15+
816
## [UNRELEASED]
917

1018
### Changes

pkg-py/src/querychat/_icons.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import Literal
2+
3+
from shiny import ui
4+
5+
ICON_NAMES = Literal["funnel-fill", "terminal-fill", "table"]
6+
7+
8+
def bs_icon(name: ICON_NAMES) -> ui.HTML:
9+
"""Get Bootstrap icon SVG by name."""
10+
if name not in BS_ICONS:
11+
raise ValueError(f"Unknown Bootstrap icon: {name}")
12+
return ui.HTML(BS_ICONS[name])
13+
14+
15+
BS_ICONS = {
16+
"funnel-fill": '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-funnel-fill" viewBox="0 0 16 16"><path d="M1.5 1.5A.5.5 0 0 1 2 1h12a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.128.334L10 8.692V13.5a.5.5 0 0 1-.342.474l-3 1A.5.5 0 0 1 6 14.5V8.692L1.628 3.834A.5.5 0 0 1 1.5 3.5z"/></svg>',
17+
"terminal-fill": '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="bi bi-terminal-fill " style="height:1em;width:1em;fill:currentColor;vertical-align:-0.125em;" aria-hidden="true" role="img" ><path d="M0 3a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V3zm9.5 5.5h-3a.5.5 0 0 0 0 1h3a.5.5 0 0 0 0-1zm-6.354-.354a.5.5 0 1 0 .708.708l2-2a.5.5 0 0 0 0-.708l-2-2a.5.5 0 1 0-.708.708L4.793 6.5 3.146 8.146z"></path></svg>',
18+
"table": '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="bi bi-table " style="height:1em;width:1em;fill:currentColor;vertical-align:-0.125em;" aria-hidden="true" role="img" ><path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V2zm15 2h-4v3h4V4zm0 4h-4v3h4V8zm0 4h-4v3h3a1 1 0 0 0 1-1v-2zm-5 3v-3H6v3h4zm-5 0v-3H1v2a1 1 0 0 0 1 1h3zm-4-4h4V8H1v3zm0-4h4V4H1v3zm5-3v3h4V4H6zm4 4H6v3h4V8z"></path></svg>',
19+
}

pkg-py/src/querychat/_querychat.py

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import warnings
55
from typing import TYPE_CHECKING, Literal, Optional, overload
66

7-
from shiny import Inputs, Outputs, Session, module, ui
7+
from shiny import App, Inputs, Outputs, Session, module, reactive, render, req, ui
88
from shiny.session import get_current_session
9+
from shinychat import output_markdown_stream
910

11+
from ._icons import bs_icon
1012
from ._querychat_impl import (
1113
ServerResult,
1214
init_impl,
@@ -131,6 +133,99 @@ def __init__(
131133
self._server_initialized: bool = False
132134
self._values: ServerResult | None = None
133135

136+
def app(
137+
self, *, bookmark_store: Literal["url", "server", "disable"] = "url"
138+
) -> App:
139+
"""
140+
Quickly chat with a dataset.
141+
142+
Creates a Shiny app with a chat sidebar and data table view -- providing a
143+
quick-and-easy way to start chatting with your data.
144+
145+
Parameters
146+
----------
147+
bookmark_store
148+
The bookmarking store to use for the Shiny app. Options are:
149+
- `"url"`: Store bookmarks in the URL (default).
150+
- `"server"`: Store bookmarks on the server.
151+
- `"disable"`: Disable bookmarking.
152+
153+
Returns
154+
-------
155+
:
156+
A Shiny App object that can be run with `app.run()` or served with `shiny run`.
157+
158+
"""
159+
enable_bookmarking = bookmark_store != "disable"
160+
table_name = self.data_source.table_name
161+
162+
def app_ui(request):
163+
return ui.page_sidebar(
164+
self.sidebar(),
165+
ui.card(
166+
ui.card_header(
167+
ui.div(
168+
ui.div(
169+
bs_icon("terminal-fill"),
170+
ui.output_text("query_title", inline=True),
171+
class_="d-flex align-items-center gap-2",
172+
),
173+
ui.output_ui("ui_reset", inline=True),
174+
class_="hstack gap-3",
175+
),
176+
),
177+
ui.output_ui("sql_output"),
178+
fill=False,
179+
style="max-height: 33%;",
180+
),
181+
ui.card(
182+
ui.card_header(bs_icon("table"), " Data"),
183+
ui.output_data_frame("dt"),
184+
),
185+
title=ui.span("querychat with ", ui.code(table_name)),
186+
class_="bslib-page-dashboard",
187+
fillable=True,
188+
)
189+
190+
def app_server(input: Inputs, output: Outputs, session: Session):
191+
self._server(enable_bookmarking=enable_bookmarking)
192+
193+
@render.text
194+
def query_title():
195+
return self.title() or "SQL Query"
196+
197+
@render.ui
198+
def ui_reset():
199+
req(self.sql())
200+
return ui.input_action_button(
201+
"reset_query",
202+
"Reset Query",
203+
class_="btn btn-outline-danger btn-sm lh-1 ms-auto",
204+
)
205+
206+
@reactive.effect
207+
@reactive.event(input.reset_query)
208+
def _():
209+
self.sql("")
210+
self.title(None)
211+
212+
@render.data_frame
213+
def dt():
214+
return self.df()
215+
216+
@render.ui
217+
def sql_output():
218+
sql = self.sql() or f"SELECT * FROM {table_name}"
219+
sql_code = f"```sql\n{sql}\n```"
220+
return output_markdown_stream(
221+
"sql_code",
222+
content=sql_code,
223+
auto_scroll=False,
224+
width="100%",
225+
)
226+
227+
return App(app_ui, app_server, bookmark_store=bookmark_store)
228+
134229
def sidebar(
135230
self,
136231
*,
@@ -186,7 +281,7 @@ def _ui_wrapper(**ui_kwargs):
186281

187282
return _ui_wrapper(self._id, **kwargs)
188283

189-
def _server(self):
284+
def _server(self, *, enable_bookmarking: bool = True) -> None:
190285
"""
191286
Initialize the server module.
192287
@@ -195,17 +290,7 @@ def _server(self):
195290
This is a private method since it is called automatically in Express mode.
196291
197292
"""
198-
# No-op if already initialized
199-
if self._server_initialized:
200-
warnings.warn(
201-
f"QueryChat server logic for instance '{self._id}' has already "
202-
"been initialized. Subsequent calls to .server() are no-ops.",
203-
UserWarning,
204-
stacklevel=2,
205-
)
206-
return
207-
208-
# Needs be called within an active Shiny session
293+
# Needs to be called within an active Shiny session
209294
session = get_current_session()
210295
if session is None:
211296
raise RuntimeError(
@@ -231,6 +316,7 @@ def mod_server_wrapper(
231316
system_prompt=self.system_prompt,
232317
greeting=self.greeting,
233318
client=self.client,
319+
enable_bookmarking=enable_bookmarking,
234320
)
235321

236322
# Call the server module

pkg-py/src/querychat/_querychat_impl.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import chatlas
2121
import pandas as pd
2222
from narwhals.stable.v1.typing import IntoFrame
23+
from shiny.bookmark import BookmarkState, RestoreState
2324

2425

2526
ReactiveString = reactive.Value[str]
@@ -178,10 +179,12 @@ def server_impl(
178179
system_prompt: str,
179180
greeting: Optional[str],
180181
client: chatlas.Chat,
182+
enable_bookmarking: bool = False,
181183
) -> ServerResult:
182184
# Reactive values to store state
183185
current_title = ReactiveStringOrNone(None)
184186
current_query = ReactiveString("")
187+
has_greeted = reactive.value[bool](False) # noqa: FBT003
185188

186189
@reactive.calc
187190
def filtered_df():
@@ -239,6 +242,9 @@ def _():
239242

240243
@reactive.effect
241244
async def greet_on_startup():
245+
if has_greeted():
246+
return
247+
242248
if greeting:
243249
await chat_ui.append_message(greeting)
244250
elif greeting is None:
@@ -248,6 +254,30 @@ async def greet_on_startup():
248254
)
249255
await chat_ui.append_message_stream(stream)
250256

257+
has_greeted.set(True)
258+
259+
if enable_bookmarking:
260+
chat_ui.enable_bookmarking(client)
261+
262+
def _on_bookmark(x: BookmarkState) -> None:
263+
vals = x.values # noqa: PD011
264+
vals["querychat_current_query"] = current_query.get()
265+
vals["querychat_current_title"] = current_title.get()
266+
vals["querychat_has_greeted"] = has_greeted.get()
267+
268+
session.bookmark.on_bookmark(_on_bookmark)
269+
270+
def _on_restore(x: RestoreState) -> None:
271+
vals = x.values # noqa: PD011
272+
if "querychat_current_query" in vals:
273+
current_query.set(vals["querychat_current_query"])
274+
if "querychat_current_title" in vals:
275+
current_title.set(vals["querychat_current_title"])
276+
if "querychat_has_greeted" in vals:
277+
has_greeted.set(vals["querychat_has_greeted"])
278+
279+
session.bookmark.on_restore(_on_restore)
280+
251281
return ServerResult(
252282
df=filtered_df,
253283
current_query=current_query,

pkg-py/src/querychat/tools.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import chevron
77
from chatlas import ContentToolResult, Tool
8-
from htmltools import HTML
98
from shinychat.types import ToolResultDisplay
109

10+
from ._icons import bs_icon
1111
from ._utils import df_to_html
1212

1313
if TYPE_CHECKING:
@@ -66,9 +66,7 @@ def update_dashboard(query: str, title: str) -> ContentToolResult:
6666
title=title,
6767
show_request=False,
6868
open=True,
69-
icon=HTML(
70-
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-funnel-fill" viewBox="0 0 16 16"><path d="M1.5 1.5A.5.5 0 0 1 2 1h12a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.128.334L10 8.692V13.5a.5.5 0 0 1-.342.474l-3 1A.5.5 0 0 1 6 14.5V8.692L1.628 3.834A.5.5 0 0 1 1.5 3.5z"/></svg>',
71-
),
69+
icon=bs_icon("funnel-fill"),
7270
),
7371
},
7472
)
@@ -142,9 +140,7 @@ def reset_dashboard() -> ContentToolResult:
142140
title=None,
143141
show_request=False,
144142
open=False,
145-
icon=HTML(
146-
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="bi bi-arrow-counterclockwise" style="height:1em;width:1em;fill:currentColor;vertical-align:-0.125em;" aria-hidden="true" role="img"><path fill-rule="evenodd" d="M8 3a5 5 0 1 1-4.546 2.914.5.5 0 0 0-.908-.417A6 6 0 1 0 8 2v1z"></path><path d="M8 4.466V.534a.25.25 0 0 0-.41-.192L5.23 2.308a.25.25 0 0 0 0 .384l2.36 1.966A.25.25 0 0 0 8 4.466z"></path></svg>',
147-
),
143+
icon=bs_icon("terminal-fill"),
148144
),
149145
},
150146
)
@@ -213,9 +209,7 @@ def query(query: str, _intent: str = "") -> ContentToolResult:
213209
markdown=markdown,
214210
show_request=False,
215211
open=True,
216-
icon=HTML(
217-
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-table" viewBox="0 0 16 16"><path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm15 2h-4v3h4zm0 4h-4v3h4zm0 4h-4v3h3a1 1 0 0 0 1-1zm-5 3v-3H6v3zm-5 0v-3H1v2a1 1 0 0 0 1 1zm-4-4h4V8H1zm0-4h4V4H1zm5-3v3h4V4zm4 4H6v3h4z"/></svg>',
218-
),
212+
icon=bs_icon("table"),
219213
),
220214
},
221215
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ maintainers = [
2121
dependencies = [
2222
"duckdb",
2323
"pandas",
24-
"shiny",
24+
"shiny @ git+https://github.com/posit-dev/py-shiny.git@fix/bookmark-missing-input-error",
2525
"shinywidgets",
2626
"htmltools",
2727
"chatlas>=0.12.0",

0 commit comments

Comments
 (0)