st-show-more 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- st_show_more-0.1.3/PKG-INFO +94 -0
- st_show_more-0.1.3/README.md +75 -0
- st_show_more-0.1.3/pyproject.toml +35 -0
- st_show_more-0.1.3/setup.cfg +4 -0
- st_show_more-0.1.3/st_show_more/__init__.py +123 -0
- st_show_more-0.1.3/st_show_more/frontend/show_more.css +98 -0
- st_show_more-0.1.3/st_show_more/frontend/show_more.js +59 -0
- st_show_more-0.1.3/st_show_more.egg-info/PKG-INFO +94 -0
- st_show_more-0.1.3/st_show_more.egg-info/SOURCES.txt +11 -0
- st_show_more-0.1.3/st_show_more.egg-info/dependency_links.txt +1 -0
- st_show_more-0.1.3/st_show_more.egg-info/requires.txt +5 -0
- st_show_more-0.1.3/st_show_more.egg-info/top_level.txt +1 -0
- st_show_more-0.1.3/tests/test_preview.py +33 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: st_show_more
|
|
3
|
+
Version: 0.1.3
|
|
4
|
+
Summary: A Streamlit component that writes text like st.write, truncated to a preview with an inline "Show more" button.
|
|
5
|
+
Keywords: streamlit,streamlit-component,show more,read more,truncate
|
|
6
|
+
Classifier: Development Status :: 4 - Beta
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Topic :: Software Development :: User Interfaces
|
|
12
|
+
Classifier: Topic :: Software Development :: Widget Sets
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
Requires-Dist: streamlit>=1.51
|
|
16
|
+
Requires-Dist: markdown-it-py>=3.0
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest; extra == "dev"
|
|
19
|
+
|
|
20
|
+
# st_show_more
|
|
21
|
+
|
|
22
|
+
A Streamlit component that writes Markdown text like `st.write`, collapsed to a
|
|
23
|
+
preview that ends with an inline **Show more** button. Clicking it reveals the
|
|
24
|
+
full text.
|
|
25
|
+
|
|
26
|
+
Expanding and collapsing happens in the browser: no rerun of your app, and the
|
|
27
|
+
state survives reruns triggered by other widgets.
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install git+https://github.com/informatica92/st_show_more.git
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Requires Streamlit >= 1.51 (it is built on `st.components.v2`, so it renders
|
|
36
|
+
inline in the page, not in an iframe, and follows the app theme).
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from st_show_more import st_show_more
|
|
42
|
+
|
|
43
|
+
# Preview = first 120 characters of the text
|
|
44
|
+
st_show_more(long_text, preview=120)
|
|
45
|
+
|
|
46
|
+
# Preview = your own text, custom button labels
|
|
47
|
+
st_show_more(long_text, preview="A short summary.", more_label="Read all", less_label="Close")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
| Parameter | Type | Default | Description |
|
|
51
|
+
|---------------------|-----------------|---------------|-------------|
|
|
52
|
+
| `text` | `str` | required | Full text (Markdown). |
|
|
53
|
+
| `preview` | `str` or `int` | `200` | `int`: first N characters of `text`, cut at a word boundary and followed by `…`. If `text` fits in N characters it is shown in full with no button. `str`: used as the preview as-is (Markdown). |
|
|
54
|
+
| `more_label` | `str` | `"Show more"` | Label of the expand button. |
|
|
55
|
+
| `less_label` | `str` or `None` | `"Show less"` | Label of the collapse button. `None` = no way back once expanded. |
|
|
56
|
+
| `color` | `str` or `None` | `None` | Text color, any CSS color: `"gray"`, `"blue"`, `"#ff4b4b"`... `None` = theme text color. Links and the button keep the theme's link color. |
|
|
57
|
+
| `size` | number, `str` or `None` | `None` | Font size. A number is pixels (`12`), a string is a CSS length (`"0.875rem"`). `None` = app default. |
|
|
58
|
+
| `expanded` | `bool` | `False` | Initial state. |
|
|
59
|
+
| `unsafe_allow_html` | `bool` | `False` | Render raw HTML found in the text instead of escaping it. Only for trusted content. |
|
|
60
|
+
| `key` | `str` or `None` | `None` | Unique key of the instance. |
|
|
61
|
+
|
|
62
|
+
## Notes
|
|
63
|
+
|
|
64
|
+
- Markdown is rendered with [markdown-it-py](https://github.com/executablebooks/markdown-it-py)
|
|
65
|
+
(CommonMark + tables + strikethrough). Streamlit-only syntax such as
|
|
66
|
+
`:red[text]`, `:material/icon:` and emoji shortcodes is not supported.
|
|
67
|
+
- An `int` preview cuts the raw Markdown source, so a cut in the middle of
|
|
68
|
+
`**bold**` or a link shows the unfinished markup literally. Pass a `str`
|
|
69
|
+
preview when the beginning of your text is heavily formatted.
|
|
70
|
+
- Links open in a new tab.
|
|
71
|
+
|
|
72
|
+
## Development
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install -e .[dev]
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pytest
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
streamlit run example.py
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## TODO
|
|
87
|
+
|
|
88
|
+
- [x] Rename the package to `st_show_more`, following the naming used by most Streamlit custom components.
|
|
89
|
+
- [ ] Rename the GitHub repo to `st_show_more` (the install URL above already uses it) and update the `origin` remote.
|
|
90
|
+
- [ ] Push a version tag (e.g. `v0.1.0`) so dependents can pin a release.
|
|
91
|
+
- [ ] Protect the `main` branch.
|
|
92
|
+
- [ ] Refine this README.
|
|
93
|
+
- [ ] Push to PyPI.
|
|
94
|
+
- [ ] Add "Open in streamlit" badges to the examples.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# st_show_more
|
|
2
|
+
|
|
3
|
+
A Streamlit component that writes Markdown text like `st.write`, collapsed to a
|
|
4
|
+
preview that ends with an inline **Show more** button. Clicking it reveals the
|
|
5
|
+
full text.
|
|
6
|
+
|
|
7
|
+
Expanding and collapsing happens in the browser: no rerun of your app, and the
|
|
8
|
+
state survives reruns triggered by other widgets.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install git+https://github.com/informatica92/st_show_more.git
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Requires Streamlit >= 1.51 (it is built on `st.components.v2`, so it renders
|
|
17
|
+
inline in the page, not in an iframe, and follows the app theme).
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from st_show_more import st_show_more
|
|
23
|
+
|
|
24
|
+
# Preview = first 120 characters of the text
|
|
25
|
+
st_show_more(long_text, preview=120)
|
|
26
|
+
|
|
27
|
+
# Preview = your own text, custom button labels
|
|
28
|
+
st_show_more(long_text, preview="A short summary.", more_label="Read all", less_label="Close")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
| Parameter | Type | Default | Description |
|
|
32
|
+
|---------------------|-----------------|---------------|-------------|
|
|
33
|
+
| `text` | `str` | required | Full text (Markdown). |
|
|
34
|
+
| `preview` | `str` or `int` | `200` | `int`: first N characters of `text`, cut at a word boundary and followed by `…`. If `text` fits in N characters it is shown in full with no button. `str`: used as the preview as-is (Markdown). |
|
|
35
|
+
| `more_label` | `str` | `"Show more"` | Label of the expand button. |
|
|
36
|
+
| `less_label` | `str` or `None` | `"Show less"` | Label of the collapse button. `None` = no way back once expanded. |
|
|
37
|
+
| `color` | `str` or `None` | `None` | Text color, any CSS color: `"gray"`, `"blue"`, `"#ff4b4b"`... `None` = theme text color. Links and the button keep the theme's link color. |
|
|
38
|
+
| `size` | number, `str` or `None` | `None` | Font size. A number is pixels (`12`), a string is a CSS length (`"0.875rem"`). `None` = app default. |
|
|
39
|
+
| `expanded` | `bool` | `False` | Initial state. |
|
|
40
|
+
| `unsafe_allow_html` | `bool` | `False` | Render raw HTML found in the text instead of escaping it. Only for trusted content. |
|
|
41
|
+
| `key` | `str` or `None` | `None` | Unique key of the instance. |
|
|
42
|
+
|
|
43
|
+
## Notes
|
|
44
|
+
|
|
45
|
+
- Markdown is rendered with [markdown-it-py](https://github.com/executablebooks/markdown-it-py)
|
|
46
|
+
(CommonMark + tables + strikethrough). Streamlit-only syntax such as
|
|
47
|
+
`:red[text]`, `:material/icon:` and emoji shortcodes is not supported.
|
|
48
|
+
- An `int` preview cuts the raw Markdown source, so a cut in the middle of
|
|
49
|
+
`**bold**` or a link shows the unfinished markup literally. Pass a `str`
|
|
50
|
+
preview when the beginning of your text is heavily formatted.
|
|
51
|
+
- Links open in a new tab.
|
|
52
|
+
|
|
53
|
+
## Development
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install -e .[dev]
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pytest
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
streamlit run example.py
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## TODO
|
|
68
|
+
|
|
69
|
+
- [x] Rename the package to `st_show_more`, following the naming used by most Streamlit custom components.
|
|
70
|
+
- [ ] Rename the GitHub repo to `st_show_more` (the install URL above already uses it) and update the `origin` remote.
|
|
71
|
+
- [ ] Push a version tag (e.g. `v0.1.0`) so dependents can pin a release.
|
|
72
|
+
- [ ] Protect the `main` branch.
|
|
73
|
+
- [ ] Refine this README.
|
|
74
|
+
- [ ] Push to PyPI.
|
|
75
|
+
- [ ] Add "Open in streamlit" badges to the examples.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "st_show_more"
|
|
7
|
+
version = "0.1.3"
|
|
8
|
+
description = "A Streamlit component that writes text like st.write, truncated to a preview with an inline \"Show more\" button."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
keywords = ["streamlit", "streamlit-component", "show more", "read more", "truncate"]
|
|
12
|
+
# Must be valid trove classifiers (https://pypi.org/classifiers/): PyPI rejects
|
|
13
|
+
# the upload otherwise. There is no "Framework :: Streamlit" classifier.
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Topic :: Software Development :: User Interfaces",
|
|
21
|
+
"Topic :: Software Development :: Widget Sets",
|
|
22
|
+
]
|
|
23
|
+
dependencies = [
|
|
24
|
+
"streamlit>=1.51",
|
|
25
|
+
"markdown-it-py>=3.0",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.optional-dependencies]
|
|
29
|
+
dev = ["pytest"]
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.packages.find]
|
|
32
|
+
include = ["st_show_more*"]
|
|
33
|
+
|
|
34
|
+
[tool.setuptools.package-data]
|
|
35
|
+
st_show_more = ["frontend/*"]
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""st_show_more: st.write-like text with an inline "Show more" button."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import streamlit as st
|
|
8
|
+
from markdown_it import MarkdownIt
|
|
9
|
+
|
|
10
|
+
__all__ = ["st_show_more", "make_preview"]
|
|
11
|
+
__version__ = "0.1.0"
|
|
12
|
+
|
|
13
|
+
_FRONTEND = Path(__file__).parent / "frontend"
|
|
14
|
+
|
|
15
|
+
# Registered once at import time; every st_show_more() call mounts a new instance.
|
|
16
|
+
_component = st.components.v2.component(
|
|
17
|
+
"st_show_more.show_more",
|
|
18
|
+
html='<div class="ssm-root"></div>',
|
|
19
|
+
css=(_FRONTEND / "show_more.css").read_text(encoding="utf-8"),
|
|
20
|
+
js=(_FRONTEND / "show_more.js").read_text(encoding="utf-8"),
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# Raw HTML in the text is escaped unless the caller opts in (same default as st.write).
|
|
24
|
+
_MD_SAFE = MarkdownIt("commonmark", {"html": False, "linkify": False}).enable(["table", "strikethrough"])
|
|
25
|
+
_MD_HTML = MarkdownIt("commonmark", {"html": True, "linkify": False}).enable(["table", "strikethrough"])
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def make_preview(text: str, n_chars: int, ellipsis: str = "…") -> tuple[str, bool]:
|
|
29
|
+
"""Cut ``text`` to at most ``n_chars`` characters, preferring a word boundary.
|
|
30
|
+
|
|
31
|
+
Returns ``(preview, truncated)``. When the text already fits, it is returned
|
|
32
|
+
unchanged with ``truncated=False``.
|
|
33
|
+
"""
|
|
34
|
+
if n_chars < 0:
|
|
35
|
+
raise ValueError("preview length must be >= 0")
|
|
36
|
+
if len(text) <= n_chars:
|
|
37
|
+
return text, False
|
|
38
|
+
cut = text[:n_chars]
|
|
39
|
+
# Don't break a word in half, unless that would throw away most of the preview.
|
|
40
|
+
if not text[n_chars].isspace():
|
|
41
|
+
boundary = max(cut.rfind(" "), cut.rfind("\n"))
|
|
42
|
+
if boundary >= n_chars * 0.6:
|
|
43
|
+
cut = cut[:boundary]
|
|
44
|
+
# "elit,…" reads badly: drop dangling punctuation before the ellipsis.
|
|
45
|
+
return cut.rstrip().rstrip(",;:.-–—") + ellipsis, True
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def st_show_more(
|
|
49
|
+
text: str,
|
|
50
|
+
preview: str | int = 200,
|
|
51
|
+
more_label: str = "Show more",
|
|
52
|
+
less_label: str | None = "Show less",
|
|
53
|
+
*,
|
|
54
|
+
color: str | None = None,
|
|
55
|
+
size: int | float | str | None = None,
|
|
56
|
+
expanded: bool = False,
|
|
57
|
+
unsafe_allow_html: bool = False,
|
|
58
|
+
key: str | None = None,
|
|
59
|
+
) -> None:
|
|
60
|
+
"""Write ``text`` as Markdown, collapsed to a preview with a "Show more" button.
|
|
61
|
+
|
|
62
|
+
Parameters
|
|
63
|
+
----------
|
|
64
|
+
text : str
|
|
65
|
+
The full text (Markdown), shown once the user clicks the button.
|
|
66
|
+
preview : str or int
|
|
67
|
+
What to show while collapsed. An ``int`` takes the first N characters of
|
|
68
|
+
``text`` (cut at a word boundary, with a trailing ellipsis); if ``text``
|
|
69
|
+
is not longer than N it is shown in full, without a button. A ``str`` is
|
|
70
|
+
used as the preview as-is (Markdown).
|
|
71
|
+
more_label : str
|
|
72
|
+
Label of the button that expands the text.
|
|
73
|
+
less_label : str or None
|
|
74
|
+
Label of the button that collapses the text again. ``None`` makes the
|
|
75
|
+
expansion one-way (no button once expanded).
|
|
76
|
+
color : str or None
|
|
77
|
+
Text color, as any CSS color: a name (``"gray"``, ``"blue"``), a hex
|
|
78
|
+
code (``"#ff4b4b"``) or a function (``"rgb(...)"``). ``None`` uses the
|
|
79
|
+
theme's text color. Links and the button keep the theme's link color.
|
|
80
|
+
size : int, float, str or None
|
|
81
|
+
Font size. A number is taken as pixels (``12`` -> ``12px``); a string is
|
|
82
|
+
used as a CSS length (``"0.875rem"``). ``None`` uses the app's size.
|
|
83
|
+
expanded : bool
|
|
84
|
+
Initial state.
|
|
85
|
+
unsafe_allow_html : bool
|
|
86
|
+
Render raw HTML contained in the text instead of escaping it. Only
|
|
87
|
+
enable this for content you trust.
|
|
88
|
+
key : str or None
|
|
89
|
+
Optional unique key for the component instance.
|
|
90
|
+
"""
|
|
91
|
+
text = "" if text is None else str(text)
|
|
92
|
+
|
|
93
|
+
# bool is an int subclass: reject it rather than treating True as "1 char".
|
|
94
|
+
if isinstance(preview, bool) or not isinstance(preview, (int, str)):
|
|
95
|
+
raise TypeError("preview must be a str or an int (number of characters)")
|
|
96
|
+
if isinstance(preview, int):
|
|
97
|
+
preview_text, truncated = make_preview(text, preview)
|
|
98
|
+
else:
|
|
99
|
+
preview_text, truncated = preview, preview != text
|
|
100
|
+
|
|
101
|
+
if color is not None and not isinstance(color, str):
|
|
102
|
+
raise TypeError("color must be a CSS color string")
|
|
103
|
+
if isinstance(size, bool) or not isinstance(size, (int, float, str, type(None))):
|
|
104
|
+
raise TypeError("size must be a number of pixels or a CSS length string")
|
|
105
|
+
if isinstance(size, (int, float)):
|
|
106
|
+
if size <= 0:
|
|
107
|
+
raise ValueError("size must be > 0")
|
|
108
|
+
size = f"{size:g}px"
|
|
109
|
+
|
|
110
|
+
md = _MD_HTML if unsafe_allow_html else _MD_SAFE
|
|
111
|
+
_component(
|
|
112
|
+
key=key,
|
|
113
|
+
data={
|
|
114
|
+
"full_html": md.render(text),
|
|
115
|
+
"preview_html": md.render(preview_text) if truncated else "",
|
|
116
|
+
"truncated": truncated,
|
|
117
|
+
"more_label": more_label,
|
|
118
|
+
"less_label": less_label,
|
|
119
|
+
"color": color,
|
|
120
|
+
"size": size,
|
|
121
|
+
"expanded": bool(expanded),
|
|
122
|
+
},
|
|
123
|
+
)
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
.ssm-root {
|
|
2
|
+
font-family: var(--st-font, inherit);
|
|
3
|
+
color: var(--st-text-color, inherit);
|
|
4
|
+
line-height: 1.6;
|
|
5
|
+
overflow-wrap: break-word;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.ssm-root > :first-child {
|
|
9
|
+
margin-top: 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.ssm-root > :last-child {
|
|
13
|
+
margin-bottom: 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.ssm-root p,
|
|
17
|
+
.ssm-root ul,
|
|
18
|
+
.ssm-root ol,
|
|
19
|
+
.ssm-root pre,
|
|
20
|
+
.ssm-root blockquote,
|
|
21
|
+
.ssm-root table {
|
|
22
|
+
margin: 0 0 1rem;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.ssm-root h1,
|
|
26
|
+
.ssm-root h2,
|
|
27
|
+
.ssm-root h3,
|
|
28
|
+
.ssm-root h4,
|
|
29
|
+
.ssm-root h5,
|
|
30
|
+
.ssm-root h6 {
|
|
31
|
+
font-family: var(--st-heading-font, inherit);
|
|
32
|
+
line-height: 1.2;
|
|
33
|
+
margin: 1rem 0 0.5rem;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.ssm-root a {
|
|
37
|
+
color: var(--st-link-color, inherit);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.ssm-root code,
|
|
41
|
+
.ssm-root pre {
|
|
42
|
+
font-family: var(--st-code-font, monospace);
|
|
43
|
+
font-size: 0.875em;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.ssm-root :not(pre) > code {
|
|
47
|
+
background: var(--st-secondary-background-color, transparent);
|
|
48
|
+
border-radius: 0.25rem;
|
|
49
|
+
padding: 0.1em 0.3em;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.ssm-root pre {
|
|
53
|
+
background: var(--st-secondary-background-color, transparent);
|
|
54
|
+
border-radius: 0.5rem;
|
|
55
|
+
padding: 0.75rem;
|
|
56
|
+
overflow-x: auto;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.ssm-root blockquote {
|
|
60
|
+
border-left: 2px solid var(--st-border-color, currentColor);
|
|
61
|
+
padding-left: 1rem;
|
|
62
|
+
opacity: 0.8;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.ssm-root img {
|
|
66
|
+
max-width: 100%;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.ssm-root table {
|
|
70
|
+
border-collapse: collapse;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.ssm-root th,
|
|
74
|
+
.ssm-root td {
|
|
75
|
+
border: 1px solid var(--st-border-color, currentColor);
|
|
76
|
+
padding: 0.25rem 0.5rem;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.ssm-toggle {
|
|
80
|
+
background: none;
|
|
81
|
+
border: none;
|
|
82
|
+
padding: 0;
|
|
83
|
+
font: inherit;
|
|
84
|
+
font-weight: 600;
|
|
85
|
+
color: var(--st-link-color, inherit);
|
|
86
|
+
cursor: pointer;
|
|
87
|
+
white-space: nowrap;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
.ssm-toggle:hover {
|
|
91
|
+
text-decoration: underline;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.ssm-toggle:focus-visible {
|
|
95
|
+
outline: 2px solid var(--st-primary-color, currentColor);
|
|
96
|
+
outline-offset: 2px;
|
|
97
|
+
border-radius: 2px;
|
|
98
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Expanded/collapsed state per instance. Kept on the client so toggling never
|
|
2
|
+
// triggers a Streamlit rerun, and kept outside the DOM so it survives remounts.
|
|
3
|
+
const states = new Map();
|
|
4
|
+
|
|
5
|
+
export default function (component) {
|
|
6
|
+
const { data, parentElement, key } = component;
|
|
7
|
+
const root = parentElement.querySelector(".ssm-root");
|
|
8
|
+
if (!root || !data) return;
|
|
9
|
+
|
|
10
|
+
const { full_html, preview_html, truncated, more_label, less_label } = data;
|
|
11
|
+
|
|
12
|
+
// Assigned through the style API: an invalid value is simply ignored and
|
|
13
|
+
// the stylesheet default applies.
|
|
14
|
+
root.style.color = "";
|
|
15
|
+
root.style.fontSize = "";
|
|
16
|
+
if (data.color) root.style.color = data.color;
|
|
17
|
+
if (data.size) root.style.fontSize = data.size;
|
|
18
|
+
|
|
19
|
+
// A different text is a different thing to read: start from the default again.
|
|
20
|
+
const signature = `${preview_html}\u0000${full_html}`;
|
|
21
|
+
const id = key ?? root;
|
|
22
|
+
let state = states.get(id);
|
|
23
|
+
if (!state || state.signature !== signature || state.initial !== data.expanded) {
|
|
24
|
+
state = { signature, initial: data.expanded, expanded: Boolean(data.expanded) };
|
|
25
|
+
states.set(id, state);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const render = () => {
|
|
29
|
+
const expanded = state.expanded || !truncated;
|
|
30
|
+
root.innerHTML = expanded ? full_html : preview_html;
|
|
31
|
+
root.querySelectorAll("a").forEach((a) => {
|
|
32
|
+
a.target = "_blank";
|
|
33
|
+
a.rel = "noopener noreferrer";
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const label = expanded ? less_label : more_label;
|
|
37
|
+
if (!truncated || !label) return;
|
|
38
|
+
|
|
39
|
+
const button = document.createElement("button");
|
|
40
|
+
button.type = "button";
|
|
41
|
+
button.className = "ssm-toggle";
|
|
42
|
+
button.textContent = label;
|
|
43
|
+
button.setAttribute("aria-expanded", String(expanded));
|
|
44
|
+
button.addEventListener("click", () => {
|
|
45
|
+
state.expanded = !expanded;
|
|
46
|
+
render();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Keep the button on the same line as the end of the text when possible.
|
|
50
|
+
const last = root.lastElementChild;
|
|
51
|
+
if (last && last.tagName === "P") {
|
|
52
|
+
last.append(" ", button);
|
|
53
|
+
} else {
|
|
54
|
+
root.append(button);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
render();
|
|
59
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: st_show_more
|
|
3
|
+
Version: 0.1.3
|
|
4
|
+
Summary: A Streamlit component that writes text like st.write, truncated to a preview with an inline "Show more" button.
|
|
5
|
+
Keywords: streamlit,streamlit-component,show more,read more,truncate
|
|
6
|
+
Classifier: Development Status :: 4 - Beta
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Topic :: Software Development :: User Interfaces
|
|
12
|
+
Classifier: Topic :: Software Development :: Widget Sets
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
Requires-Dist: streamlit>=1.51
|
|
16
|
+
Requires-Dist: markdown-it-py>=3.0
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest; extra == "dev"
|
|
19
|
+
|
|
20
|
+
# st_show_more
|
|
21
|
+
|
|
22
|
+
A Streamlit component that writes Markdown text like `st.write`, collapsed to a
|
|
23
|
+
preview that ends with an inline **Show more** button. Clicking it reveals the
|
|
24
|
+
full text.
|
|
25
|
+
|
|
26
|
+
Expanding and collapsing happens in the browser: no rerun of your app, and the
|
|
27
|
+
state survives reruns triggered by other widgets.
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install git+https://github.com/informatica92/st_show_more.git
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Requires Streamlit >= 1.51 (it is built on `st.components.v2`, so it renders
|
|
36
|
+
inline in the page, not in an iframe, and follows the app theme).
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from st_show_more import st_show_more
|
|
42
|
+
|
|
43
|
+
# Preview = first 120 characters of the text
|
|
44
|
+
st_show_more(long_text, preview=120)
|
|
45
|
+
|
|
46
|
+
# Preview = your own text, custom button labels
|
|
47
|
+
st_show_more(long_text, preview="A short summary.", more_label="Read all", less_label="Close")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
| Parameter | Type | Default | Description |
|
|
51
|
+
|---------------------|-----------------|---------------|-------------|
|
|
52
|
+
| `text` | `str` | required | Full text (Markdown). |
|
|
53
|
+
| `preview` | `str` or `int` | `200` | `int`: first N characters of `text`, cut at a word boundary and followed by `…`. If `text` fits in N characters it is shown in full with no button. `str`: used as the preview as-is (Markdown). |
|
|
54
|
+
| `more_label` | `str` | `"Show more"` | Label of the expand button. |
|
|
55
|
+
| `less_label` | `str` or `None` | `"Show less"` | Label of the collapse button. `None` = no way back once expanded. |
|
|
56
|
+
| `color` | `str` or `None` | `None` | Text color, any CSS color: `"gray"`, `"blue"`, `"#ff4b4b"`... `None` = theme text color. Links and the button keep the theme's link color. |
|
|
57
|
+
| `size` | number, `str` or `None` | `None` | Font size. A number is pixels (`12`), a string is a CSS length (`"0.875rem"`). `None` = app default. |
|
|
58
|
+
| `expanded` | `bool` | `False` | Initial state. |
|
|
59
|
+
| `unsafe_allow_html` | `bool` | `False` | Render raw HTML found in the text instead of escaping it. Only for trusted content. |
|
|
60
|
+
| `key` | `str` or `None` | `None` | Unique key of the instance. |
|
|
61
|
+
|
|
62
|
+
## Notes
|
|
63
|
+
|
|
64
|
+
- Markdown is rendered with [markdown-it-py](https://github.com/executablebooks/markdown-it-py)
|
|
65
|
+
(CommonMark + tables + strikethrough). Streamlit-only syntax such as
|
|
66
|
+
`:red[text]`, `:material/icon:` and emoji shortcodes is not supported.
|
|
67
|
+
- An `int` preview cuts the raw Markdown source, so a cut in the middle of
|
|
68
|
+
`**bold**` or a link shows the unfinished markup literally. Pass a `str`
|
|
69
|
+
preview when the beginning of your text is heavily formatted.
|
|
70
|
+
- Links open in a new tab.
|
|
71
|
+
|
|
72
|
+
## Development
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install -e .[dev]
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pytest
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
streamlit run example.py
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## TODO
|
|
87
|
+
|
|
88
|
+
- [x] Rename the package to `st_show_more`, following the naming used by most Streamlit custom components.
|
|
89
|
+
- [ ] Rename the GitHub repo to `st_show_more` (the install URL above already uses it) and update the `origin` remote.
|
|
90
|
+
- [ ] Push a version tag (e.g. `v0.1.0`) so dependents can pin a release.
|
|
91
|
+
- [ ] Protect the `main` branch.
|
|
92
|
+
- [ ] Refine this README.
|
|
93
|
+
- [ ] Push to PyPI.
|
|
94
|
+
- [ ] Add "Open in streamlit" badges to the examples.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
st_show_more/__init__.py
|
|
4
|
+
st_show_more.egg-info/PKG-INFO
|
|
5
|
+
st_show_more.egg-info/SOURCES.txt
|
|
6
|
+
st_show_more.egg-info/dependency_links.txt
|
|
7
|
+
st_show_more.egg-info/requires.txt
|
|
8
|
+
st_show_more.egg-info/top_level.txt
|
|
9
|
+
st_show_more/frontend/show_more.css
|
|
10
|
+
st_show_more/frontend/show_more.js
|
|
11
|
+
tests/test_preview.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
st_show_more
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from st_show_more import make_preview
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_short_text_is_untouched():
|
|
7
|
+
assert make_preview("hello", 10) == ("hello", False)
|
|
8
|
+
assert make_preview("hello", 5) == ("hello", False)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_cuts_at_word_boundary():
|
|
12
|
+
assert make_preview("the quick brown fox jumps", 18) == ("the quick brown…", True)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_cut_falling_exactly_between_words_keeps_whole_word():
|
|
16
|
+
assert make_preview("the quick brown fox", 9) == ("the quick…", True)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_long_word_is_cut_hard():
|
|
20
|
+
assert make_preview("supercalifragilistic", 8) == ("supercal…", True)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_dangling_punctuation_is_dropped():
|
|
24
|
+
assert make_preview("adipiscing elit, sed do eiusmod", 18) == ("adipiscing elit…", True)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_zero_chars():
|
|
28
|
+
assert make_preview("hello", 0) == ("…", True)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_negative_length_is_rejected():
|
|
32
|
+
with pytest.raises(ValueError):
|
|
33
|
+
make_preview("hello", -1)
|