Skip to content

feat(r): Generic datasources (full PR) #29

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

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ __pycache__/
animation.screenflow/
README_files/
README.html
.DS_Store
python-package/examples/titanic.db
.quarto

# Byte-compiled / optimized / DLL files
Expand Down
14 changes: 7 additions & 7 deletions python-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def server(input, output, session):
# chat["df"]() reactive.
@render.data_frame
def data_table():
return chat["df"]()
return chat.df()


# Create Shiny app
Expand Down Expand Up @@ -171,8 +171,8 @@ which you can then pass via:

```python
querychat_config = querychat.init(
df=titanic,
table_name="titanic",
titanic,
"titanic",
data_description=Path("data_description.md").read_text()
)
```
Expand All @@ -185,8 +185,8 @@ You can add additional instructions of your own to the end of the system prompt,

```python
querychat_config = querychat.init(
df=titanic,
table_name="titanic",
titanic,
"titanic",
extra_instructions=[
"You're speaking to a British audience--please use appropriate spelling conventions.",
"Use lots of emojis! 😃 Emojis everywhere, 🌍 emojis forever. ♾️",
Expand Down Expand Up @@ -218,8 +218,8 @@ def my_chat_func(system_prompt: str) -> chatlas.Chat:
my_chat_func = partial(chatlas.ChatAnthropic, model="claude-3-7-sonnet-latest")

querychat_config = querychat.init(
df=titanic,
table_name="titanic",
titanic,
"titanic",
create_chat_callback=my_chat_func
)
```
Expand Down
57 changes: 57 additions & 0 deletions python-package/examples/app-database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from pathlib import Path

from seaborn import load_dataset
from shiny import App, render, ui
from sqlalchemy import create_engine

import querychat

# Load titanic data and create SQLite database
db_path = Path(__file__).parent / "titanic.db"
engine = create_engine("sqlite:///" + str(db_path))

if not db_path.exists():
# For example purposes, we'll create the database if it doesn't exist. Don't
# do this in your app!
titanic = load_dataset("titanic")
titanic.to_sql("titanic", engine, if_exists="replace", index=False)

greeting = (Path(__file__).parent / "greeting.md").read_text()
data_desc = (Path(__file__).parent / "data_description.md").read_text()

# 1. Configure querychat
querychat_config = querychat.init(
engine,
"titanic",
greeting=greeting,
data_description=data_desc,
)

# Create UI
app_ui = ui.page_sidebar(
# 2. Place the chat component in the sidebar
querychat.sidebar("chat"),
# Main panel with data viewer
ui.card(
ui.output_data_frame("data_table"),
fill=True,
),
title="querychat with Python (SQLite)",
fillable=True,
)


# Define server logic
def server(input, output, session):
# 3. Initialize querychat server with the config from step 1
chat = querychat.server("chat", querychat_config)

# 4. Display the filtered dataframe
@render.data_frame
def data_table():
# Access filtered data via chat.df() reactive
return chat["df"]()


# Create Shiny app
app = App(app_ui, server)
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

titanic = load_dataset("titanic")

with open(Path(__file__).parent / "greeting.md", "r") as f:
greeting = f.read()
with open(Path(__file__).parent / "data_description.md", "r") as f:
data_desc = f.read()
greeting = (Path(__file__).parent / "greeting.md").read_text()
data_desc = (Path(__file__).parent / "data_description.md").read_text()

# 1. Configure querychat
querychat_config = querychat.init(
Expand Down Expand Up @@ -43,7 +41,7 @@ def server(input, output, session):
@render.data_frame
def data_table():
# Access filtered data via chat.df() reactive
return chat["df"]()
return chat.df()


# Create Shiny app
Expand Down
10 changes: 8 additions & 2 deletions python-package/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ dependencies = [
"htmltools",
"chatlas",
"narwhals",
"chevron",
"sqlalchemy>=2.0.0" # Using 2.0+ for improved type hints and API
]

classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
Expand All @@ -42,7 +43,12 @@ packages = ["src/querychat"]
include = ["src/querychat", "LICENSE", "README.md"]

[tool.uv]
dev-dependencies = ["ruff>=0.6.5", "pyright>=1.1.401", "tox-uv>=1.11.4"]
dev-dependencies = [
"ruff>=0.6.5",
"pyright>=1.1.401",
"tox-uv>=1.11.4",
"pytest>=8.4.0",
]

[tool.ruff]
src = ["src/querychat"]
Expand Down
Loading