Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from chatlas import ChatBedrockAnthropic

from shiny import reactive
from shiny.express import ui
from shiny.express import input, ui

# Either explicitly set the AWS environment variables before launching the app, or set
# them in a file named `.env`. The `python-dotenv` package will load `.env` as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from chatlas import ChatAzureOpenAI

from shiny import reactive
from shiny.express import ui
from shiny.express import input, ui

# ChatAzureOpenAI() requires an API key from Azure OpenAI.
# See the docs for more information on how to obtain one.
Expand Down
2 changes: 1 addition & 1 deletion shiny/templates/markdown-stream/llms/ollama/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# ChatOllama() requires an Ollama model server to be running locally.
# See the docs for more information on how to set up a local Ollama server.
# https://posit-dev.github.io/chatlas/reference/ChatOllama.html
chat_client = ChatOllama()
chat_client = ChatOllama(model="llama3.2")

# Some sidebar input controls to populate a prompt and trigger the stream
with ui.sidebar():
Expand Down
8 changes: 6 additions & 2 deletions tests/playwright/examples/example_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,12 @@ def wait_for_idle_app(
)


def validate_example(page: Page, ex_app_path: str) -> None:
sa: ShinyAppProc = run_shiny_app(pyshiny_root / ex_app_path, wait_for_start=True)
def validate_example(
page: Page, ex_app_path: str, *, env: dict[str, str] | None = None
) -> None:
sa: ShinyAppProc = run_shiny_app(
pyshiny_root / ex_app_path, wait_for_start=True, env=env
)
app_name = os.path.basename(os.path.dirname(ex_app_path))
short_app_path = (
f"{os.path.basename(os.path.dirname(os.path.dirname(ex_app_path)))}/{app_name}"
Expand Down
56 changes: 56 additions & 0 deletions tests/playwright/examples/template_provider_stubs/sitecustomize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Stub LLM constructors before a template app starts in its child process.

Python imports sitecustomize from PYTHONPATH before running ``python -m shiny``.
This keeps the test's provider patches in the process that imports the app.
"""

import importlib
import importlib.util
import sys
import types

import pytest

from shiny.types import Jsonifiable


class DummyChatClient:
def __init__(self, *args: object, **kwargs: object) -> None:
pass

async def stream_async(self, *args: object, **kwargs: object) -> None:
raise AssertionError("Template unexpectedly called an LLM provider")

def stream(self, *args: object, **kwargs: object) -> None:
raise AssertionError("Template unexpectedly called an LLM provider")

async def get_state(self) -> Jsonifiable:
return {}

async def set_state(self, value: Jsonifiable) -> None:
pass


monkeypatch = pytest.MonkeyPatch()

if importlib.util.find_spec("langchain_openai") is None:
stub = types.ModuleType("langchain_openai")
stub.__dict__["ChatOpenAI"] = DummyChatClient
monkeypatch.setitem(sys.modules, "langchain_openai", stub)
else:
langchain_openai = importlib.import_module("langchain_openai")
monkeypatch.setattr(langchain_openai, "ChatOpenAI", DummyChatClient)

if importlib.util.find_spec("chatlas") is not None:
import chatlas

for name in (
"ChatOpenAI",
"ChatAnthropic",
"ChatGoogle",
"ChatOllama",
"ChatAzureOpenAI",
"ChatBedrockAnthropic",
):
if hasattr(chatlas, name):
monkeypatch.setattr(chatlas, name, DummyChatClient)
34 changes: 34 additions & 0 deletions tests/playwright/examples/test_shiny_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from shiny._main._create import (
GithubRepoLocation,
find_templates,
parse_github_arg,
shiny_internal_templates,
template_by_name,
Expand Down Expand Up @@ -86,6 +87,39 @@ def test_template_examples(page: Page, ex_app_path: str) -> None:
validate_example(page, ex_app_path)


def template_entrypoints() -> list[str]:
"""All runnable bundled templates, including nested chat templates."""
root = Path(__file__).parents[3]
cases: list[str] = []
for template in find_templates(root / "shiny" / "templates"):
if template.type == "package" or template.path.parent.name == "app":
# The app templates already run in test_template_examples.
continue
for name in ("app.py", "app-core.py", "app-express.py"):
path = template.path / name
if path.is_file():
relative_path = str(path.relative_to(root))
cases.append(relative_path)
assert cases
return cases


@pytest.mark.parametrize("ex_app_path", template_entrypoints())
def test_bundled_template_starts_without_errors(page: Page, ex_app_path: str) -> None:
stubs = Path(__file__).parent / "template_provider_stubs"
pythonpath = os.pathsep.join(
filter(None, (str(stubs), os.environ.get("PYTHONPATH")))
)
env = {
"PYTHONPATH": pythonpath,
"OPENAI_API_KEY": "dummy",
"ANTHROPIC_API_KEY": "dummy",
"GOOGLE_API_KEY": "dummy",
"AZURE_OPENAI_API_KEY": "dummy",
}
validate_example(page, ex_app_path, env=env)


app_templates = [t.id for t in shiny_internal_templates.apps]
pkg_templates = [t.id for t in shiny_internal_templates.packages]
assert len(app_templates) > 0
Expand Down
Loading