-
Notifications
You must be signed in to change notification settings - Fork 20
feat(pkg-py): Add querychat.app(), enable bookmarking by default
#99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+207
−12
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| """Convenience function for creating a simple querychat app.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Literal, Optional | ||
|
|
||
| import chatlas | ||
| from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui | ||
| from shinychat import output_markdown_stream | ||
|
|
||
| from ._icons import bs_icon | ||
| from .datasource import DataSource | ||
| from .querychat import QueryChatConfig, _resolve_querychat_client, init | ||
| from .querychat import mod_server as server | ||
| from .querychat import sidebar | ||
|
|
||
| if TYPE_CHECKING: | ||
| import sqlalchemy | ||
| from narwhals.stable.v1.typing import IntoFrame | ||
|
|
||
|
|
||
| def app( | ||
| x: IntoFrame | sqlalchemy.Engine | QueryChatConfig | DataSource, | ||
| *, | ||
| name: Optional[str] = None, | ||
| client: Optional[str | chatlas.Chat] = None, | ||
| bookmark_store: Literal["url", "server", "disable"] = "url", | ||
| **kwargs, | ||
| ) -> App: | ||
| """ | ||
| Quickly chat with a dataset. | ||
|
|
||
| Creates a Shiny app with a chat sidebar and data table view -- providing a | ||
| quick-and-easy way to start chatting with your data. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| x | ||
| The dataset to chat with. Can be any one of the following: | ||
| - A Narwhals-compatible data frame (e.g., Polars or Pandas) | ||
| - A SQLAlchemy engine containing the table to query against. | ||
| - The result of `querychat.init()` (for more advanced configuration). | ||
| - A `DataSource` object. | ||
| name | ||
| The name of the dataset. | ||
| client | ||
| A `chatlas.Chat` object or a string to be passed to `chatlas.ChatAuto()` | ||
| describing the model to use (e.g. `"openai/gpt-4.1"`). If no client is | ||
| provided, querychat will look for the `QUERYCHAT_CLIENT` environment | ||
| variable. If that variable is not set, it will default to | ||
| `chatlas.ChatOpenAI()`. | ||
| bookmark_store | ||
| The bookmarking store to use for the Shiny app. Options are: | ||
| - `"url"`: Store bookmarks in the URL (default). | ||
| - `"server"`: Store bookmarks on the server. | ||
| - `"disable"`: Disable bookmarking. | ||
| **kwargs | ||
| Additional keyword arguments to pass to `querychat.init()` if `x` | ||
| is not already a QueryChatConfig object. | ||
|
|
||
| Returns | ||
| ------- | ||
| App | ||
| A Shiny App object that can be run with `app.run()` or served with `shiny run`. | ||
|
|
||
| """ | ||
| if not isinstance(x, QueryChatConfig): | ||
| data_source = x.get_data() if isinstance(x, DataSource) else x | ||
| x = init(data_source, table_name=name or "data", **kwargs) | ||
|
|
||
| if client is not None: | ||
| x.client = _resolve_querychat_client(client) | ||
|
|
||
| enable_bookmarking = bookmark_store != "disable" | ||
|
|
||
| def app_ui(request): | ||
| return ui.page_sidebar( | ||
| sidebar("chat"), | ||
| ui.card( | ||
| ui.card_header( | ||
| ui.div( | ||
| ui.div( | ||
| bs_icon("terminal-fill"), | ||
| ui.output_text("query_title", inline=True), | ||
| class_="d-flex align-items-center gap-2", | ||
| ), | ||
| ui.output_ui("ui_reset", inline=True), | ||
| class_="hstack gap-3", | ||
| ), | ||
| ), | ||
| ui.output_ui("sql_output"), | ||
| fill=False, | ||
| style="max-height: 33%;", | ||
| ), | ||
| ui.card( | ||
| ui.card_header(bs_icon("table"), " Data"), | ||
| ui.output_data_frame("dt"), | ||
| ), | ||
| title=ui.span( | ||
| "querychat with ", | ||
| ui.code(x.data_source.table_name), | ||
| ), | ||
| class_="bslib-page-dashboard", | ||
| fillable=True, | ||
| ) | ||
|
|
||
| def app_server(input: Inputs, output: Outputs, session: Session): | ||
| qc = server("chat", x, enable_bookmarking=enable_bookmarking) | ||
|
|
||
| @render.text | ||
| def query_title(): | ||
| return qc.title() or "SQL Query" | ||
|
|
||
| @render.ui | ||
| def ui_reset(): | ||
| req(qc.sql()) | ||
| return ui.input_action_button( | ||
| "reset_query", | ||
| "Reset Query", | ||
| class_="btn btn-outline-danger btn-sm lh-1 ms-auto", | ||
| ) | ||
|
|
||
| @reactive.effect | ||
| @reactive.event(input.reset_query) | ||
| def _(): | ||
| qc.sql("") | ||
| qc.title(None) | ||
|
|
||
| @render.data_frame | ||
| def dt(): | ||
| return qc.df() | ||
|
|
||
| @render.ui | ||
| def sql_output(): | ||
| sql = qc.sql() or f"SELECT * FROM {x.data_source.table_name}" | ||
| sql_code = f"```sql\n{sql}\n```" | ||
| return output_markdown_stream( | ||
| "sql_code", | ||
| content=sql_code, | ||
| auto_scroll=False, | ||
| width="100%", | ||
| ) | ||
|
|
||
| return App(app_ui, app_server, bookmark_store=bookmark_store) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from typing import Literal | ||
|
|
||
| from shiny import ui | ||
|
|
||
| ICON_NAMES = Literal["funnel-fill", "terminal-fill", "table"] | ||
|
|
||
|
|
||
| def bs_icon(name: ICON_NAMES) -> ui.HTML: | ||
| """Get Bootstrap icon SVG by name.""" | ||
| if name not in BS_ICONS: | ||
| raise ValueError(f"Unknown Bootstrap icon: {name}") | ||
| return ui.HTML(BS_ICONS[name]) | ||
|
|
||
|
|
||
| BS_ICONS = { | ||
| "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>', | ||
| "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>', | ||
| "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>', | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once posit-dev/py-shiny#2117 gets merged/released
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't want to make release a requirement, should probably make bookmarking an opt-in for now