Hide/collapse/fold a section

Is it possible to fold a section and all its nested sections, items, plots etc.?

5 Likes

Sort of. You can use a st.checkbox. If it’s checked you run the code otherwise not.

1 Like

This approach I already used, but I was wondering whether there’s something more canonical.

3 Likes

At the moment I’d say @Marc’s comment reflects the state of Streamlit as a whole: You can use widgets to trigger running one section of code, which means you could potentially use a multiselect or even a slider, theoretically speaking.

At the moment we don’t have anything that fully speaks to the idea of controls that hide/show particular widgets.

I’ve made a feature request for show/hide controls here.

1 Like

Think that situation:
I want to have a c++ editable code, but code components are not editable, then I add a button edit to put the code into a text_area to edit it, I dont want to show both components at the same time.

Today I lost a lot of time trying to do that, but I think that if streamlit had a hide/show property that situacion wil became easy to implement.

I find it tricky in Streamlit to work with buttons if clicking on them should trigger something that is reflected in the GUI afterwards. I came up with the following example using radio buttons and the SessionState module that should come close to what you are trying to do. You will find SessionState module here, in case you don’t have it: https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92
Hope that helps.

import streamlit as st
import SessionState

session = SessionState.get(code='some c++ code')
a = st.radio("Edit or show", ['Edit', 'Show'], 1)
if a == 'Edit':
    session.code = st.text_input('Edit code', session.code)
else:
    st.write(session.code)
2 Likes

@godot63 thank you, I like your solution, it is the exactly behaivior that I need.
I am amazed at the simplicity of SessionState implementation, so easy to understand and modify. I think that the idea diserve a star.

A lot of thanks.

I found another way to solve my situation, using streamlit.empty as a hideable object.

import streamlit as st

code = read()
edit = st.checkbox("Edit")
ph = st.empty()
code = ph.text_area("Your code here 👇", code)
if not edit:
    ph.empty()
    save(code)
    st.code(code, "c++")

read and save are methods to read and write a file where the code is being persisted.
If I do not add the persistence I lose the information when unchecking the checkbox

hi @stdevRulo, I think this is a very good solution as well. I had something similar in mind too but then thought that you might want to have the user choose if he wants to save the text. Maybe you combine the session state solution and add a save button which then saves the code interactively, using your save function?