Skip to content
Merged
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
4 changes: 4 additions & 0 deletions pkg-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [UNRELEASED]

* Fixed an issue with the query tool when used with SQLAlchemy data sources. (@npelikan #79)

## [0.2.0] - 2025-09-02

* `querychat.init()` now accepts a `client` argument, replacing the previous `create_chat_callback` argument. (#60)
Expand Down
12 changes: 8 additions & 4 deletions pkg-py/src/querychat/querychat.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,15 @@ def df_to_html(df: IntoFrame, maxrows: int = 5) -> str:
HTML string representation of the table

"""
if isinstance(df, (nw.LazyFrame, nw.DataFrame)):
df_short = df.lazy().head(maxrows).collect()
nrow_full = df.lazy().select(nw.len()).collect().item()
ndf = nw.from_native(df)

if isinstance(ndf, (nw.LazyFrame, nw.DataFrame)):
df_short = ndf.lazy().head(maxrows).collect()
nrow_full = ndf.lazy().select(nw.len()).collect().item()
else:
raise TypeError("df must be a Narwhals DataFrame or LazyFrame")
raise TypeError(
"Must be able to convert `df` into a Narwhals DataFrame or LazyFrame",
)

# Generate HTML table
table_html = df_short.to_pandas().to_html(
Expand Down
116 changes: 116 additions & 0 deletions pkg-py/tests/test_df_to_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import sqlite3
import tempfile
from pathlib import Path

import pandas as pd
import pytest
from sqlalchemy import create_engine
from src.querychat.datasource import DataFrameSource, SQLAlchemySource
from src.querychat.querychat import df_to_html


@pytest.fixture
def sample_dataframe():
"""Create a sample pandas DataFrame for testing."""
return pd.DataFrame(
{
"id": [1, 2, 3, 4, 5],
"name": ["Alice", "Bob", "Charlie", "Diana", "Eve"],
"age": [25, 30, 35, 28, 32],
"salary": [50000, 60000, 70000, 55000, 65000],
},
)


@pytest.fixture
def sample_sqlite():
"""Create a temporary SQLite database with test data."""
temp_db = tempfile.NamedTemporaryFile(delete=False, suffix=".db") # noqa: SIM115
temp_db.close()

conn = sqlite3.connect(temp_db.name)
cursor = conn.cursor()

cursor.execute("""
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER,
salary REAL
)
""")

test_data = [
(1, "Alice", 25, 50000),
(2, "Bob", 30, 60000),
(3, "Charlie", 35, 70000),
(4, "Diana", 28, 55000),
(5, "Eve", 32, 65000),
]

cursor.executemany(
"INSERT INTO employees (id, name, age, salary) VALUES (?, ?, ?, ?)",
test_data,
)

conn.commit()
conn.close()

engine = create_engine(f"sqlite:///{temp_db.name}")
yield engine

# Cleanup
Path(temp_db.name).unlink()


def test_df_to_html_with_dataframe_source_result(sample_dataframe):
"""Test that df_to_html() works with results from DataFrameSource.execute_query()."""
source = DataFrameSource(sample_dataframe, "employees")

# Execute query to get pandas DataFrame
result_df = source.execute_query("SELECT * FROM employees WHERE age > 25")

# This should succeed after the fix
html_output = df_to_html(result_df)

# Verify the HTML contains expected content
assert isinstance(html_output, str)
assert "<table" in html_output
assert "Bob" in html_output
assert "Charlie" in html_output
assert "Diana" in html_output
assert "Eve" in html_output


def test_df_to_html_with_sqlalchemy_source_result(sample_sqlite):
"""Test that df_to_html() works with results from SQLAlchemySource.execute_query()."""
source = SQLAlchemySource(sample_sqlite, "employees")

# Execute query to get pandas DataFrame
result_df = source.execute_query("SELECT * FROM employees WHERE age > 25")

# This should succeed after the fix
html_output = df_to_html(result_df)

# Verify the HTML contains expected content
assert isinstance(html_output, str)
assert "<table" in html_output
assert "Bob" in html_output
assert "Charlie" in html_output
assert "Diana" in html_output
assert "Eve" in html_output


def test_df_to_html_with_truncation(sample_dataframe):
"""Test that df_to_html() properly truncates large datasets."""
source = DataFrameSource(sample_dataframe, "employees")

# Execute query to get all rows
result_df = source.execute_query("SELECT * FROM employees")

# Test with maxrows=3 to trigger truncation
html_output = df_to_html(result_df, maxrows=3)

# Should show truncation message
assert "Showing only the first 3 rows out of 5" in html_output
assert "<table" in html_output
2 changes: 1 addition & 1 deletion pkg-r/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Remotes:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.3
2 changes: 1 addition & 1 deletion pkg-r/man/querychat_app.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.