Skip to content

Commit fe56ddc

Browse files
committed
[chore] Update the README
1 parent eab014f commit fe56ddc

File tree

4 files changed

+118
-69
lines changed

4 files changed

+118
-69
lines changed

README.md

Lines changed: 94 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,103 @@ For complete information, please see the [Streamlit Components documentation](ht
66

77
## Overview
88

9-
A Streamlit Component is made out of a Python API and a frontend (built using any web tech you prefer).
10-
11-
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.
12-
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`.
9+
Components pair a Python API with a frontend. For new projects, we recommend the v2 API; the v1 API is also supported if you prefer it.
7510

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

78-
See the `template-reactless` directory for a template that does not use [React](https://reactjs.org/).
67+
See full examples in `templates/v1/template/` and `templates/v1/template-reactless/`.
68+
69+
## Supported template versions
70+
71+
This repo provides templates for both Streamlit Component APIs:
72+
73+
- v2: Uses `st.components.v2.component()` and `@streamlit/component-v2-lib`.
74+
- v1: Uses `st.components.v1.component()` and `streamlit-component-lib`.
75+
76+
## Quickstart (generate a new component with Cookiecutter)
77+
78+
- 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.
79+
- Generate from this template using Cookiecutter via `uvx`:
80+
81+
- v2 (recommended):
82+
83+
```bash
84+
uvx --from cookiecutter cookiecutter gh:streamlit/component-template --directory cookiecutter/v2
85+
```
86+
87+
- v1:
88+
```bash
89+
uvx --from cookiecutter cookiecutter gh:streamlit/component-template --directory cookiecutter/v1
90+
```
91+
92+
- Follow the interactive prompts to generate your project.
93+
94+
## Just browsing? Pre-generated outputs
95+
96+
If you want to quickly explore without running Cookiecutter, browse the pre-generated templates in this repo:
97+
98+
- v2 outputs: `templates/v2/template/` and `templates/v2/template-reactless/`
99+
- v1 outputs: `templates/v1/template/` and `templates/v1/template-reactless/`
100+
101+
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.
102+
103+
## Examples
79104

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

82107
## Community-provided Templates
83108

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

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/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)