This repo contains a template and example code for creating Streamlit components using Svelte (+ Typescript) for the component's frontend.
It replicates the component from the Streamlit official templates. It relies on Svelte Rollup template and is inspired by the use of Svelte in Fanilo Andrianasolo's Named Entity Selection Component.
For complete information, please see the Streamlit Components documentation!
A Streamlit Component is made out of a Python API and a frontend (built using any web tech you prefer).
A Component can be used in any Streamlit app, can pass data between Python and frontend code, and can optionally be distributed on PyPI for the rest of the world to use.
import streamlit.components.v1 as components
# Declare the component:
my_component = components.declare_component("my_component", path="frontend/public")
# Use it:
my_component(greeting="Hello", name="World")
<script lang="ts">
import { setStreamlitLifecycle } from "./streamlit";
setStreamlitLifecycle();
// Access arguments from Python
export let greeting: string;
export let name: string;
</script>
<div>{greeting}, {name}!</div>
$ python3 -m venv venv # create venv
$ . venv/bin/activate # activate venv
$ pip install streamlit # install streamlit
$ cd my_component/frontend
$ npm install # Install npm dependencies
$ npm run dev # Start the rollup dev server
$ . venv/bin/activate # activate the venv you created earlier
$ streamlit run my_component/__init__.py # run the example
my_component/frontend/src/MyComponent.svelte
.my_component/__init__.py
.