Skip to content

Commit 1d4534d

Browse files
committed
Add output_code UI component and tests
1 parent 21c9631 commit 1d4534d

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from shiny import App, Inputs, Outputs, Session, render, ui
2+
3+
app_ui = ui.page_fluid(
4+
ui.input_text_area(
5+
"source",
6+
"Enter code to display below:",
7+
"print('Hello, Shiny!')\nfor i in range(3):\n print(i)",
8+
rows=8,
9+
),
10+
ui.card(
11+
ui.output_code("code_default"),
12+
),
13+
ui.card(
14+
ui.output_code("code_no_placeholder", placeholder=False),
15+
),
16+
)
17+
18+
19+
def server(input: Inputs, output: Outputs, session: Session):
20+
@render.code
21+
def code_default():
22+
return input.source()
23+
24+
@render.code
25+
def code_no_placeholder():
26+
return input.source()
27+
28+
29+
app = App(app_ui, server)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from shiny.express import input, output_args, render, ui
2+
3+
ui.input_text_area(
4+
"source",
5+
"Enter code to display below:",
6+
"print('Hello, Shiny!')\nfor i in range(3):\n print(i)",
7+
rows=8,
8+
)
9+
10+
with ui.card():
11+
12+
@render.code
13+
def code_default():
14+
return input.source()
15+
16+
17+
with ui.card():
18+
19+
@render.code(placeholder=False)
20+
def code_no_placeholder():
21+
return input.source()

shiny/ui/_output.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from htmltools import Tag, TagAttrValue, TagFunction, css, div, tags
1616

17-
from .._docstring import add_example, no_example
17+
from .._docstring import add_example
1818
from ..module import resolve_id
1919
from ..types import MISSING, MISSING_TYPE
2020
from ._plot_output_opts import (
@@ -271,7 +271,7 @@ def output_text(
271271
return container(id=resolve_id(id), class_="shiny-text-output")
272272

273273

274-
@no_example()
274+
@add_example()
275275
def output_code(id: str, placeholder: bool = True) -> Tag:
276276
"""
277277
Create a output container for code (monospaced text).
@@ -304,9 +304,6 @@ def output_code(id: str, placeholder: bool = True) -> Tag:
304304
* :func:`~shiny.ui.output_text`
305305
* :func:`~shiny.ui.output_text_verbatim`
306306
307-
Example
308-
-------
309-
See :func:`~shiny.ui.output_text`
310307
"""
311308

312309
cls = "shiny-text-output" + (" noplaceholder" if not placeholder else "")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from conftest import create_doc_example_core_fixture
2+
from playwright.sync_api import Page
3+
4+
from shiny.playwright import controller
5+
from shiny.run import ShinyAppProc
6+
7+
app = create_doc_example_core_fixture("output_code")
8+
9+
10+
def test_output_code_kitchen(page: Page, app: ShinyAppProc) -> None:
11+
page.goto(app.url)
12+
13+
source = controller.InputTextArea(page, "source")
14+
code_default = controller.OutputCode(page, "code_default")
15+
code_no_placeholder = controller.OutputCode(page, "code_no_placeholder")
16+
17+
source.set("")
18+
19+
code_default.expect_value("")
20+
code_default.expect_has_placeholder(True)
21+
22+
code_no_placeholder.expect_value("")
23+
code_no_placeholder.expect_has_placeholder(False)
24+
25+
new_value = "print('testing output_code')\nfor i in range(2):\n print(i)"
26+
source.set(new_value)
27+
28+
code_default.expect_value(new_value)
29+
code_no_placeholder.expect_value(new_value)

0 commit comments

Comments
 (0)