Skip to content

Commit b778e18

Browse files
[chore] Update the README (#115)
### TL;DR Updated the README with v2 component API documentation and improved development instructions. ### What changed? - Restructured the main README to prominently feature both v1 and v2 component APIs - Added code samples for both APIs with expandable sections - Updated the quickstart section to use Cookiecutter and uv for generating new components - Added information about pre-generated templates for easy browsing - Added development install instructions with `-e` flag to all template READMEs
1 parent 45eff8f commit b778e18

File tree

8 files changed

+117
-317
lines changed

8 files changed

+117
-317
lines changed

README.md

Lines changed: 93 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,74 +10,102 @@ A Streamlit Component is made out of a Python API and a frontend (built using an
1010

1111
A Component can be used in any Streamlit app, can pass data between Python and frontend code, and can optionally be distributed on [PyPI](https://pypi.org/) for the rest of the world to use.
1212

13-
- Create a component's API in a single line of Python:
14-
15-
```python
16-
import streamlit.components.v1 as components
17-
18-
# Declare the component:
19-
my_component = components.declare_component("my_component", path="frontend/build")
20-
21-
# Use it:
22-
my_component(greeting="Hello", name="World")
23-
```
24-
25-
- Build the component's frontend out of HTML and JavaScript (or TypeScript, or ClojureScript, or whatever you fancy). React is supported, but not required:
26-
27-
```tsx
28-
import React from 'react';
29-
import {
30-
withStreamlitConnection,
31-
ComponentProps,
32-
} from 'streamlit-component-lib';
33-
34-
function MyComponent({ args }: ComponentProps) {
35-
// Access arguments from Python via `props.args`:
36-
const { greeting, name } = args;
37-
return (
38-
<div>
39-
{greeting}, {name}!
40-
</div>
41-
);
42-
}
43-
44-
export default withStreamlitConnection(MyComponent);
45-
```
46-
47-
## Quickstart
48-
49-
- Ensure you have [Python 3.9+](https://www.python.org/downloads/), [Node.js](https://nodejs.org), and [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) installed.
50-
- Clone this repo.
51-
- Create a new Python virtual environment for the template:
52-
```bash
53-
$ cd template
54-
$ python3 -m venv venv # create venv
55-
$ . venv/bin/activate # activate venv
56-
$ pip install streamlit # install streamlit
57-
```
58-
- Initialize and run the component template frontend:
59-
```bash
60-
$ cd template/my_component/frontend
61-
$ npm install # Install npm dependencies
62-
$ npm run start # Start the Vite dev server
63-
```
64-
- From a separate terminal, run the template's Streamlit app:
65-
```bash
66-
$ cd template
67-
$ . venv/bin/activate # activate the venv you created earlier
68-
$ pip install -e . # install template as editable package
69-
$ streamlit run my_component/example.py # run the example
70-
```
71-
- If all goes well, you should see something like this:
72-
![Quickstart Success](quickstart.png)
73-
- Modify the frontend code at `my_component/frontend/src/MyComponent.tsx`.
74-
- Modify the Python code at `my_component/__init__.py`.
13+
```python
14+
import streamlit as st
7515

76-
## Examples
16+
# Declare the component
17+
my_component = st.components.v2.component(
18+
"your-package.your_component",
19+
js="index-*.js",
20+
html='<div class="react-root"></div>',
21+
)
22+
23+
# Use it directly or via a small wrapper
24+
value = my_component(data={"name": "World"}, default={"num_clicks": 0})
25+
```
26+
27+
```tsx
28+
import { Component } from '@streamlit/component-v2-lib';
29+
import { createRoot } from 'react-dom/client';
30+
31+
const MyComponent: Component = (args) => {
32+
const root = createRoot(args.parentElement.querySelector('.react-root'));
33+
root.render(<div>Hello, {args.data.name}!</div>);
34+
};
35+
36+
export default MyComponent;
37+
```
38+
39+
See full examples in `templates/v2/template/` and `templates/v2/template-reactless/`.
40+
41+
<details>
42+
<summary>Show v1 code samples</summary>
43+
44+
```python
45+
import streamlit.components.v1 as components
46+
47+
# Declare the component (v1)
48+
my_component = components.declare_component("my_component", path="frontend/build")
49+
50+
# Use it
51+
value = my_component(name="World", default=0)
52+
```
53+
54+
```tsx
55+
import {
56+
withStreamlitConnection,
57+
ComponentProps,
58+
} from 'streamlit-component-lib';
59+
60+
function MyComponent({ args }: ComponentProps) {
61+
return <div>Hello, {args.name}!\n</div>;
62+
}
63+
64+
export default withStreamlitConnection(MyComponent);
65+
```
66+
67+
</details>
68+
69+
See full examples in `templates/v1/template/` and `templates/v1/template-reactless/`.
7770

78-
See the `template-reactless` directory for a template that does not use [React](https://reactjs.org/).
71+
## Supported template versions
72+
73+
This repo provides templates for both Streamlit Component APIs:
74+
75+
- v2: Uses `st.components.v2.component()` and `@streamlit/component-v2-lib`.
76+
- v1: Uses `st.components.v1.component()` and `streamlit-component-lib`.
77+
78+
## Quickstart (generate a new component with Cookiecutter)
79+
80+
- Ensure you have [uv](https://docs.astral.sh/uv/), [Node.js](https://nodejs.org), and [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) installed.
81+
- Generate from this template using Cookiecutter via `uvx`:
82+
83+
- v2 (recommended):
84+
85+
```bash
86+
uvx --from cookiecutter cookiecutter gh:streamlit/component-template --directory cookiecutter/v2
87+
```
88+
89+
- v1:
90+
```bash
91+
uvx --from cookiecutter cookiecutter gh:streamlit/component-template --directory cookiecutter/v1
92+
```
93+
94+
- Follow the interactive prompts to generate your project.
95+
- Once created, follow the `README` in your new project to get started.
96+
97+
## Just browsing? Pre-generated outputs
98+
99+
If you want to quickly explore without running Cookiecutter, browse the pre-generated templates in this repo:
100+
101+
- v2 outputs: `templates/v2/template/` and `templates/v2/template-reactless/`
102+
- v1 outputs: `templates/v1/template/` and `templates/v1/template-reactless/`
103+
104+
From within one of these folders, you can inspect the Python package layout and the `my_component/frontend` code. Refer to the template-level `README` for build instructions.
105+
106+
## Examples
79107

80-
See the `examples` directory for examples on working with pandas DataFrames, integrating with third-party libraries, and more.
108+
See the `examples/v1/` directory for examples working with pandas DataFrames, integrating with third-party libraries, and more.
81109

82110
## Community-provided Templates
83111

cookiecutter/v2/{{ cookiecutter.package_name }}/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
uv pip install {{ cookiecutter.package_name }}
99
```
1010

11+
### Development install (editable)
12+
13+
When developing this component locally, install it in editable mode so Streamlit picks up code changes without rebuilding a wheel. Run this from the directory that contains `pyproject.toml`:
14+
15+
```sh
16+
uv pip install -e . --force-reinstall
17+
```
18+
1119
## Usage instructions
1220

1321
```python

cookiecutter/v2/{{ cookiecutter.package_name }}/e2e/test_template.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

quickstart.png

-140 KB
Binary file not shown.

templates/v2/template-reactless/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ Streamlit component that allows you to do X
88
uv pip install streamlit-custom-component
99
```
1010

11+
### Development install (editable)
12+
13+
When developing this component locally, install it in editable mode so Streamlit picks up code changes without rebuilding a wheel. Run this from the directory that contains `pyproject.toml`:
14+
15+
```sh
16+
uv pip install -e . --force-reinstall
17+
```
18+
1119
## Usage instructions
1220

1321
```python

templates/v2/template-reactless/e2e/test_template.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

templates/v2/template/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ Streamlit component that allows you to do X
88
uv pip install streamlit-custom-component
99
```
1010

11+
### Development install (editable)
12+
13+
When developing this component locally, install it in editable mode so Streamlit picks up code changes without rebuilding a wheel. Run this from the directory that contains `pyproject.toml`:
14+
15+
```sh
16+
uv pip install -e . --force-reinstall
17+
```
18+
1119
## Usage instructions
1220

1321
```python

0 commit comments

Comments
 (0)