Hello everyone,
I create dummy dashboard.Grid and try to realized next logic:
if dashboard.Item drag to column indexed 0 - it must return to previous location.
my code:
from streamlit_elements import elements, mui, html, dashboard
import streamlit as st
import json
st.write(st.session_state)
if 'layout_current' not in st.session_state:
st.session_state['layout_current'] = [dashboard.Item("first_item", 1, 0, 1, 1)]
st.session_state['rerun'] = False
with elements("dashboard"):
def on_drag_stop_element(updated_layout):
if not any(map(lambda x: x['x']==0, updated_layout)):
st.session_state['layout_current'] = updated_layout
else:
st.session_state['rerun'] = True
layout = st.session_state['layout_current']
with dashboard.Grid(layout,
onLayoutChange=on_drag_stop_element,
style={"backgroundColor":"lightgrey"}):
mui.Paper("First item", key="first_item")
if st.session_state['rerun']:
st.session_state['rerun'] = False
st.rerun()
I don’t know if this is what you are looking to accomplish, it is not clear in your post.
from streamlit_elements import elements, mui, dashboard
import streamlit as st
import json
# Inicializar el estado de la sesiĂłn si es la primera ejecuciĂłn
if 'layout_current' not in st.session_state:
st.session_state['layout_current'] = [dashboard.Item("first_item", 1, 0, 1, 1)]
st.session_state['previous_layout'] = []
# Función para manejar el cambio en el diseño
def on_layout_change(updated_layout):
if any(item['x'] == 0 for item in updated_layout): # Si hay algĂşn elemento en el Ăndice 0
st.session_state['layout_current'] = st.session_state['previous_layout'] # Restaurar diseño anterior
else:
st.session_state['previous_layout'] = st.session_state['layout_current'] # Guardar diseño actual
st.session_state['layout_current'] = updated_layout # Actualizar diseño actual
# Visualizar los Ăndices
st.write("ĂŤndices actuales:")
st.json(updated_layout)
# Obtener el diseño actual
layout = st.session_state['layout_current']
# Crear la interfaz de usuario con el elemento de dashboard
with elements("dashboard"):
with dashboard.Grid(layout, onLayoutChange=on_layout_change, style={"backgroundColor": "lightgrey"}):
mui.Paper("First item", key="first_item")
Thanks for the answer!
Yes, this is almost the logic that I need, but the problem is that if I move an element to the zero index twice in a row, the logic will not work the second time
The element must return to the range of valid values ​​every time (!= 0)
for example,
I expect the following behavior (steps for changing X-coordinate):
1 → 0 =1
1 → 2 = 2
2 → 0 = 2
2 → 0 = 2 (second time!)
2 → 0 = 2 (third and more time in a row)
2 → 3 = 3
3 → 0 = 3
3 → 1 = 1
etc…
Yes,
but if I (as a test) edit “on_layout_change” function this way (always return to fixed layout from 0)
# Función para manejar el cambio en el diseño
def on_layout_change(updated_layout):
if any(item['x'] == 0 for item in updated_layout): # Si hay algĂşn elemento en el Ăndice 0
st.session_state['layout_current'] = [dashboard.Item("first_item", 1, 0, 1, 1)]#st.session_state['previous_layout'] # Restaurar diseño anterior
else:
st.session_state['previous_layout'] = st.session_state['layout_current'] # Guardar diseño actual
st.session_state['layout_current'] = updated_layout # Actualizar diseño actual
it still doesn’t work on the second or more trigger
Even though I don’t even set previous_layout
See this modification if it is similar to what you are looking for
from streamlit_elements import elements, mui, dashboard
import streamlit as st
# Inicializar el estado de la sesiĂłn si es la primera ejecuciĂłn
if 'layout_current' not in st.session_state:
st.session_state['layout_current'] = [dashboard.Item("first_item", 1, 0, 1, 1)]
st.session_state['previous_layout'] = []
# Función para manejar el cambio en el diseño
def on_layout_change(updated_layout):
# Obtener el diseño actual
current_layout = st.session_state['layout_current']
# Si hay algĂşn elemento en el Ăndice 0 en el diseño actual
if any(item['x'] == 0 for item in updated_layout) and current_layout[-1]['x'] != 0:
# Restaurar el diseño anterior
st.session_state['layout_current'] = st.session_state['previous_layout']
else:
# Guardar el diseño actual como diseño anterior
st.session_state['previous_layout'] = current_layout
# Actualizar el diseño actual
st.session_state['layout_current'] = updated_layout
# Visualizar los Ăndices
st.write("ĂŤndices actuales:")
st.json(updated_layout)
# Obtener el diseño actual
layout = st.session_state['layout_current']
# Crear la interfaz de usuario con el elemento de dashboard
with elements("dashboard"):
with dashboard.Grid(layout, onLayoutChange=on_layout_change, style={"backgroundColor": "lightgrey"}):
mui.Paper("First item", key="first_item")
This still doesn’t work in the situation “1 → 0 = 1; 1->0 = 1” (card stay in 0 after second dragging)
I assume that the problem is in the “streamlit_elements.core.frame.elements_frame.dashboard” key, which appears in the session state after dragging cards, but I haven’t figured out how to use it yet
my code now:
I replaced “OnLayoutChange” method with method “onDragStop”
I almost achieved the desired behavior, the only problem left is “stay in 0 after second drag”
from streamlit_elements import elements, mui, html, dashboard
import streamlit as st
def on_drag_stop_element(layout):
if not any(map(lambda x: x['x']==0, layout)):
st.session_state['layout_current'] = layout
# st.session_state['layout_current_new'] = layout
if any(map(lambda x: x['x']==0, layout)):
st.session_state['rerun'] = True
if 'layout_current' not in st.session_state:
# print('init')
layout = [dashboard.Item("first_item", 1, 0, 1, 1)]
st.session_state['layout_current'] = layout
st.session_state['layout_current_new'] = layout
st.session_state['rerun'] = False
# st.write(st.session_state)
with elements("dashboard"):
layout = st.session_state['layout_current_new']
with dashboard.Grid(layout,
onDragStop=on_drag_stop_element,
style={"backgroundColor":"lightgrey"}):
mui.Paper("First item", key="first_item")
if st.session_state['rerun'] == True:
st.session_state['rerun'] = False
st.session_state['layout_current_new'] = st.session_state['layout_current']
st.rerun()
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.