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
16 changes: 16 additions & 0 deletions docs/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ There is ``stlite`` directive to write Stlite app code into document.

This supports multiline strings, comma-separated strings, or combinations.

.. rst:directive:option:: height

Height of the stlite iframe.

Accepts values with units (e.g., ``500px``, ``30rem``) or numeric values (treated as pixels).

Example:

.. code-block:: rst

.. stlite::
:height: 600px

import streamlit as st
st.title("Custom Height Example")

Configuration
=============

Expand Down
2 changes: 2 additions & 0 deletions src/atsphinx/stlite/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ class StliteDirective(SphinxDirective): # noqa: D101
option_spec = {
"config": parsed_dict,
"requirements": list_of_str,
"height": str,
}
DEFAULT_OPTIONS = {
"config": None,
"requirements": [],
"height": None,
}

def run(self): # noqa: D102
Expand Down
12 changes: 11 additions & 1 deletion src/atsphinx/stlite/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@ class stlite(nodes.Element, nodes.General): # noqa: D101
def visit_stlite(self: HTML5Translator, node: stlite) -> None: # noqa: D103
config = self.builder.config
srcdoc = escape(srcdoc_template.render(app=node.attributes, config=config))

# Apply custom height if specified
style_attr = ""
if node.attributes.get("height"):
height = node.attributes["height"]
# Ensure px suffix if numeric value without unit
if height.isdigit():
height = f"{height}px"
style_attr = f' style="height: {escape(height)};"'

self.body.append(
f'<div class="stlite-wrapper"><iframe class="stlite-frame" srcdoc="{srcdoc}">'
f'<div class="stlite-wrapper"><iframe class="stlite-frame"{style_attr} srcdoc="{srcdoc}">'
)


Expand Down
44 changes: 44 additions & 0 deletions tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,47 @@ def test__parse_requirements(app: SphinxTestApp, source: str):
doctree = restructuredtext.parse(app, dedent(source).strip())
node = pick_first_stlite(doctree)
assert node["requirements"] == ["matplotlib", "polars"]


@pytest.mark.sphinx(confoverrides={"extensions": ["atsphinx.stlite"]})
@pytest.mark.parametrize(
["source", "expected"],
[
pytest.param(
"""
.. stlite::
:height: 500px

print("Hello world")
""",
"500px",
id="with-unit",
),
pytest.param(
"""
.. stlite::
:height: 30rem

print("Hello world")
""",
"30rem",
id="rem-unit",
),
pytest.param(
"""
.. stlite::
:height: 600

print("Hello world")
""",
"600",
id="numeric-only",
),
],
)
def test__parse_height(app: SphinxTestApp, source: str, expected: str):
"""Test height option parsing."""
doctree = restructuredtext.parse(app, dedent(source).strip())
node = pick_first_stlite(doctree)
assert node["height"] == expected