### Installing Streamlit Elements Library Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet provides the command to install the `streamlit-elements` library using pip. It is recommended to pin the version to `0.1.*` to ensure compatibility and avoid potential breaking API changes in future releases. ```sh pip install streamlit-elements==0.1.* ``` -------------------------------- ### Creating a Draggable and Resizable Dashboard with Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet demonstrates how to create a basic draggable and resizable dashboard using `streamlit-elements`. It defines a layout for dashboard items, specifying their initial positions, dimensions, and properties like `isDraggable` and `isResizable`. The `dashboard.Grid` context manager then renders these items within the defined layout. ```Python with elements("dashboard"): # You can create a draggable and resizable dashboard using # any element available in Streamlit Elements. from streamlit_elements import dashboard # First, build a default layout for every element you want to include in your dashboard layout = [ # Parameters: element_identifier, x_pos, y_pos, width, height, [item properties...] dashboard.Item("first_item", 0, 0, 2, 2), dashboard.Item("second_item", 2, 0, 2, 2, isDraggable=False, moved=False), dashboard.Item("third_item", 0, 2, 1, 1, isResizable=False), ] # Next, create a dashboard layout using the 'with' syntax. It takes the layout # as first parameter, plus additional properties you can find in the GitHub links below. with dashboard.Grid(layout): mui.Paper("First item", key="first_item") mui.Paper("Second item (cannot drag)", key="second_item") mui.Paper("Third item (cannot resize)", key="third_item") ``` -------------------------------- ### Displaying a Single Material UI Typography Element in Streamlit Elements Source: https://github.com/okld/streamlit-elements/blob/main/README.md This Python snippet demonstrates how to create a basic `mui.Typography` element within a Streamlit Elements frame. It shows the necessary imports and the use of the `elements` context manager to define the rendering area, then adds 'Hello world' as the text content. ```python # First, import the elements you need from streamlit_elements import elements, mui, html # Create a frame where Elements widgets will be displayed. # # Elements widgets will not render outside of this frame. # Native Streamlit widgets will not render inside this frame. # # elements() takes a key as parameter. # This key can't be reused by another frame or Streamlit widget. with elements("new_element"): # Let's create a Typography element with "Hello world" as children. # The first step is to check Typography's documentation on MUI: # https://mui.com/components/typography/ # # Here is how you would write it in React JSX: # # # Hello world # mui.Typography("Hello world") ``` -------------------------------- ### Embedding Media Player with Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet shows how to embed a versatile media player in a Streamlit application using `streamlit-elements`. It leverages `ReactPlayer` to support various third-party video and audio sources, allowing playback of content from platforms like YouTube. The `controls=True` parameter enables default player controls. ```python with elements("media_player"): # Play video from many third-party sources: YouTube, Facebook, Twitch, # SoundCloud, Streamable, Vimeo, Wistia, Mixcloud, DailyMotion and Kaltura. # # This element is powered by ReactPlayer (GitHub link below). from streamlit_elements import media media.Player(url="https://www.youtube.com/watch?v=iik25wqIuFo", controls=True) ``` -------------------------------- ### Creating Nivo Radar Chart with Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet demonstrates how to render an interactive Nivo Radar chart within a Streamlit application using `streamlit-elements`. It defines sample data and configures the chart's appearance, including margins, colors, and legends, to visualize multi-variate data. The chart is rendered inside a Material-UI Box component for proper sizing. ```python with elements("nivo_charts"): # Streamlit Elements includes 45 dataviz components powered by Nivo. from streamlit_elements import nivo DATA = [ { "taste": "fruity", "chardonay": 93, "carmenere": 61, "syrah": 114 }, { "taste": "bitter", "chardonay": 91, "carmenere": 37, "syrah": 72 }, { "taste": "heavy", "chardonay": 56, "carmenere": 95, "syrah": 99 }, { "taste": "strong", "chardonay": 64, "carmenere": 90, "syrah": 30 }, { "taste": "sunny", "chardonay": 119, "carmenere": 94, "syrah": 103 }, ] with mui.Box(sx={"height": 500}): nivo.Radar( data=DATA, keys=[ "chardonay", "carmenere", "syrah" ], indexBy="taste", valueFormat=">-.2f", margin={ "top": 70, "right": 80, "bottom": 40, "left": 80 }, borderColor={ "from": "color" }, gridLabelOffset=36, dotSize=10, dotColor={ "theme": "background" }, dotBorderWidth=2, motionConfig="wobbly", legends=[ { "anchor": "top-left", "direction": "column", "translateX": -50, "translateY": -40, "itemWidth": 80, "itemHeight": 20, "itemTextColor": "#999", "symbolSize": 12, "symbolShape": "circle", "effects": [ { "on": "hover", "style": { "itemTextColor": "#000" } } ] } ], theme={ "background": "#FFFFFF", "textColor": "#31333F", "tooltip": { "container": { "background": "#FFFFFF", "color": "#31333F" } } } ) ``` -------------------------------- ### Integrating Monaco Code Editor with Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet shows how to embed the Monaco code editor into a Streamlit application using `streamlit-elements`. It initializes the editor with a default value from `st.session_state` and uses an `onChange` callback to update the session state when the editor's content changes. A button is included to synchronize changes. ```Python with elements("monaco_editors"): # Streamlit Elements embeds Monaco code and diff editor that powers Visual Studio Code. # You can configure editor's behavior and features with the 'options' parameter. # # Streamlit Elements uses an unofficial React implementation (GitHub links below for # documentation). from streamlit_elements import editor if "content" not in st.session_state: st.session_state.content = "Default value" mui.Typography("Content: ", st.session_state.content) def update_content(value): st.session_state.content = value editor.Monaco( height=300, defaultValue=st.session_state.content, onChange=lazy(update_content) ) mui.Button("Update content", onClick=sync()) ``` -------------------------------- ### Nesting Material UI Elements with Paper and Typography Source: https://github.com/okld/streamlit-elements/blob/main/README.md This Python snippet shows how to create deeply nested elements using multiple `with` statements. It demonstrates placing `html.p` tags inside `mui.Typography`, which is itself nested within a `mui.Paper` component, illustrating complex layout composition within Streamlit Elements. ```python with elements("nested_children"): # You can nest children using multiple 'with' statements. # # # #

Hello world

#

Goodbye world

#
#
with mui.Paper: with mui.Typography: html.p("Hello world") html.p("Goodbye world") ``` -------------------------------- ### Integrating Monaco Diff Editor with Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet demonstrates how to use the Monaco diff editor within a Streamlit application via `streamlit-elements`. It takes two string inputs, `original` and `modified`, and displays their differences side-by-side, providing a visual comparison tool. ```Python editor.MonacoDiff( original="Happy Streamlit-ing!", modified="Happy Streamlit-in' with Elements!", height=300, ) ``` -------------------------------- ### Handling Dashboard Layout Changes with Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet extends the dashboard functionality by demonstrating how to capture layout changes. It defines a `handle_layout_change` callback function that is triggered when a user moves or resizes dashboard items. This callback receives the `updated_layout` and can be used to save or restore the dashboard state. ```Python def handle_layout_change(updated_layout): # You can save the layout in a file, or do anything you want with it. # You can pass it back to dashboard.Grid() if you want to restore a saved layout. print(updated_layout) with dashboard.Grid(layout, onLayoutChange=handle_layout_change): mui.Paper("First item", key="first_item") mui.Paper("Second item (cannot drag)", key="second_item") mui.Paper("Third item (cannot resize)", key="third_item") ``` -------------------------------- ### Adding Properties to Streamlit Elements MUI Components in Python Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet demonstrates how to add properties to Streamlit Elements components, specifically Material UI (MUI) widgets, using named parameters. It shows how to set properties like 'elevation', 'variant', and 'square' for 'mui.Paper' and 'label', 'defaultValue', 'variant' for 'mui.TextField'. It also illustrates how to handle Python keyword conflicts by appending an underscore to the parameter name (e.g., 'in_' for 'in'). ```python with elements("properties"): # You can add properties to elements with named parameters. # # To find all available parameters for a given element, you can # refer to its related documentation on mui.com for MUI widgets, # on https://microsoft.github.io/monaco-editor/ for Monaco editor, # and so on. # # # # with mui.Paper(elevation=3, variant="outlined", square=True): mui.TextField( label="My text input", defaultValue="Type here", variant="outlined", ) # If you must pass a parameter which is also a Python keyword, you can append an # underscore to avoid a syntax error. # # mui.Collapse(in_=True) # mui.collapse(in=True) # > Syntax error: 'in' is a Python keyword: ``` -------------------------------- ### Retrieving Element Data with Callbacks in Streamlit Elements Python Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet demonstrates how to retrieve data from an element using callbacks in Streamlit Elements. It initializes a session state item 'my_text', defines a 'handle_change' function to update 'st.session_state.my_text' with the 'event.target.value' from a 'mui.TextField', and then displays the updated text using 'mui.Typography'. The 'handle_change' function is passed to the 'onChange' property of the 'mui.TextField'. ```python import streamlit as st with elements("callbacks_retrieve_data"): # Some element allows executing a callback on specific event. # # const [name, setName] = React.useState("") # const handleChange = (event) => { # // You can see here that a text field value # // is stored in event.target.value # setName(event.target.value) # } # # # Initialize a new item in session state called "my_text" if "my_text" not in st.session_state: st.session_state.my_text = "" # When text field changes, this function will be called. # To know which parameters are passed to the callback, # you can refer to the element's documentation. def handle_change(event): st.session_state.my_text = event.target.value # Here we display what we have typed in our text field mui.Typography(st.session_state.my_text) # And here we give our 'handle_change' callback to the 'onChange' # property of the text field. mui.TextField(label="Input some text here", onChange=handle_change) ``` -------------------------------- ### Synchronizing Session State with Element Events using sync() in Streamlit Elements Python Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet demonstrates a simplified way to synchronize an element's event parameters directly with Streamlit's session state using the 'sync()' function from 'streamlit_elements'. It initializes 'st.session_state.my_event' and then uses 'onChange=sync('my_event')' on a 'mui.TextField' to automatically store the event data into 'my_event'. It also shows how to retrieve and display the 'target.value' from the synchronized event. ```python with elements("callbacks_sync"): # If you just want to store callback parameters into Streamlit's session state # like above, you can also use the special function sync(). # # When an onChange event occurs, the callback is called with an event data object # as argument. In the example below, we are synchronizing that event data object with # the session state item 'my_event'. # # If an event passes more than one parameter, you can synchronize as many session state item # as needed like so: # >>> sync("my_first_param", "my_second_param") # # If you want to ignore the first parameter of an event but keep synchronizing the second, # pass None to sync: # >>> sync(None, "second_parameter_to_keep") from streamlit_elements import sync if "my_event" not in st.session_state: st.session_state.my_event = None if st.session_state.my_event is not None: text = st.session_state.my_event.target.value else: text = "" mui.Typography(text) mui.TextField(label="Input some text here", onChange=sync("my_event")) ``` -------------------------------- ### Applying Custom CSS with 'css' Property for Generic Elements in Python Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet illustrates how to apply custom CSS styles to non-Material UI elements (generic HTML elements) within Streamlit Elements using the 'css' property. It demonstrates styling an 'html.div' component with 'backgroundColor' and a hover effect ('&:hover') using a dictionary. ```python with elements("style_elements_css"): # For any other element, use the 'css' property. # #
# This has a hotpink background #
html.div( "This has a hotpink background", css={ "backgroundColor": "hotpink", "&:hover": { "color": "lightgreen" } } ) ``` -------------------------------- ### Invoking Callbacks with Hotkeys in Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet demonstrates how to use `event.Hotkey()` to trigger a Python callback function when a specific keyboard hotkey sequence is pressed. It illustrates options like `bindInputs` to enable hotkeys within text fields and `overrideDefault` to override browser default hotkey behaviors. Note that hotkeys might not work if the Streamlit Elements frame is not in focus. ```python with elements("callbacks_hotkey"): # Invoke a callback when a specific hotkey sequence is pressed. # # For more information regarding sequences syntax and supported keys, # go to Mousetrap's project page linked below. # # /!\ Hotkeys work if you don't have focus on Streamlit Elements's frame /!\ # /!\ As with other callbacks, this reruns the whole app /!\ from streamlit_elements import event def hotkey_pressed(): print("Hotkey pressed") event.Hotkey("g", hotkey_pressed) # If you want your hotkey to work even in text fields, set bind_inputs to True. event.Hotkey("h", hotkey_pressed, bindInputs=True) mui.TextField(label="Try pressing 'h' while typing some text here.") # If you want to override default hotkeys (ie. ctrl+f to search in page), # set overrideDefault to True. event.Hotkey("ctrl+f", hotkey_pressed, overrideDefault=True) ``` -------------------------------- ### Adding Multiple Children to Material UI Button via Context Manager Source: https://github.com/okld/streamlit-elements/blob/main/README.md This Python snippet demonstrates an alternative method for adding multiple children to a `mui.Button` using a `with` statement. This approach allows for nesting children, such as Material UI icons and a `mui.Typography` component, providing a more structured way to define complex button content. ```python with elements("multiple_children"): # You can also add children to an element using a 'with' statement. # # with mui.Button: mui.icon.EmojiPeople() mui.icon.DoubleArrow() mui.Typography("Button with multiple children") ``` -------------------------------- ### Applying Custom CSS with 'sx' Property for Material UI Elements in Python Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet shows how to apply custom CSS styles to Material UI elements within Streamlit Elements using the 'sx' property. It demonstrates styling a 'mui.Box' component with properties like 'bgcolor', 'boxShadow', 'borderRadius', 'p' (padding), and 'minWidth' using a dictionary. ```python with elements("style_mui_sx"): # For Material UI elements, use the 'sx' property. # # # Some text in a styled box # mui.Box( "Some text in a styled box", sx={ "bgcolor": "background.paper", "boxShadow": 1, "borderRadius": 2, "p": 2, "minWidth": 300, } ) ``` -------------------------------- ### Invoking Callbacks Periodically with Interval() in Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet shows how to use `event.Interval()` to execute a Python callback function repeatedly at a specified time interval (in seconds). It's useful for tasks that require periodic updates or checks within a Streamlit Elements application. It's important to note that, like other callbacks, this will cause the entire app to rerun. ```python with elements("callbacks_interval"): # Invoke a callback every n seconds. # # /!\ As with other callbacks, this reruns the whole app /!\ def call_every_second(): print("Hello world") event.Interval(1, call_every_second) ``` -------------------------------- ### Deferring Callbacks with lazy() in Streamlit Elements (Python) Source: https://github.com/okld/streamlit-elements/blob/main/README.md This snippet demonstrates how to use `lazy()` to defer callback invocations, preventing the entire Streamlit app from reloading on every input change. It's particularly useful for forms where updates should only occur after a final submission or a non-lazy event. It shows how `lazy()` can wrap both `sync()` calls and regular Python functions. ```python with elements("callbacks_lazy"): # With the two first examples, each time you input a letter into the text field, # the callback is invoked but the whole app is reloaded as well. # # To avoid reloading the whole app on every input, you can wrap your callback with # lazy(). This will defer the callback invocation until another non-lazy callback # is invoked. This can be useful to implement forms. from streamlit_elements import lazy if "first_name" not in st.session_state: st.session_state.first_name = None st.session_state.last_name = None if st.session_state.first_name is not None: first_name = st.session_state.first_name.target.value else: first_name = "John" if st.session_state.last_name is not None: last_name = st.session_state.last_name.target.value else: last_name = "Doe" def set_last_name(event): st.session_state.last_name = event # Display first name and last name mui.Typography("Your first name: ", first_name) mui.Typography("Your last name: ", last_name) # Lazily synchronize onChange with first_name and last_name state. # Inputting some text won't synchronize the value yet. mui.TextField(label="First name", onChange=lazy(sync("first_name"))) # You can also pass regular python functions to lazy(). mui.TextField(label="Last name", onChange=lazy(set_last_name)) # Here we give a non-lazy callback to onClick using sync(). # We are not interested in getting onClick event data object, # so we call sync() with no argument. # # You can use either sync() or a regular python function. # As long as the callback is not wrapped with lazy(), its invocation will # also trigger every other defered callbacks. mui.Button("Update first namd and last name", onClick=sync()) ``` -------------------------------- ### Adding Multiple Children to Material UI Button via Direct Arguments Source: https://github.com/okld/streamlit-elements/blob/main/README.md This Python snippet illustrates how to add multiple children, including Material UI icons (`mui.icon.EmojiPeople`, `mui.icon.DoubleArrow`) and text, directly as arguments to a `mui.Button` component. This method provides a concise way to define the button's content. ```python with elements("multiple_children"): # You have access to Material UI icons using: mui.icon.IconNameHere # # Multiple children can be added in a single element. # # mui.Button( mui.icon.EmojiPeople, mui.icon.DoubleArrow, "Button with multiple children" ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.