finding elements within a container

  1. Are you running your app locally or is it deployed?
    1. Locally
  2. Share the link to your app’s public GitHub repository (including a requirements file).
    1. The repo is not public
  3. Share the Streamlit and Python versions.
    1. Python 3.12.4
    2. Streamlit 1.41.1

I am building a tool which dynamically creates a UI based on a JSON input. It works really well, but now I am working on doing data collection for submission to a variety of APIs, all of which I was planning on managing through callbacks. The stumbling block I am running into is that it seems as though there is no way to target a specific container to loop over all of the contents. As a matter of fact, it seems containers are purely visual and are entirely discarded after the render process. I know there are st.form objects, but I am struggling to figure out how to utilize them with the dynamic details that I need to gather and parse.

To be 100% clear, the entire layout must be dynamic and all elements must be defined within that JSON document, there is no block of code which instantiates any Streamlit objects as a hard coded block. This is a design mandate, so any solutions I find will have to fit within it.

I have figured out that I can pass the args for the callback as a list or using *args and I can get at them that way, but that requires a lot of hard definitions in the layout and I would really like to be able to target a container key and be able to get all of the children of the container.

I’m not sure I understand. Yes, containers are a visual thing, you write to it and display things in it. It’s not a widget to return values to you. However, you can write to them out of order or replace elements in them (either by rewriting them entirely, or targeting the elements directly if you kept track of them). Can you explain in more detail what you are trying to display and what process you are trying to accomplish?

I am building a UI to sit on top of a bunch of other APIs and Command line software, so I need to gather the widget values to use as arguments/inputs for those other software.

I am wanting to use containers as a grouping mechanic for looping over widgets in a clean way. Something like:
container_values = dict()
for widget_key in container:
value = st.session_state[widget_key]
container_values[widget_key] = value
foobar(container_values)

What I need is a clean way to gather all of the values of widgets within a container. Would I need to add the “container” to the session state as a list of widget keys? Is there already a built in functionality for something like this. It just feels like such a missed opportunity to not have containers be accessible to Python as, well, containers.

Would I need to add the “container” to the session state as a list of widget keys?

Yes, something like this. I recommend just keeping a dictionary in Session State to orchestrate the bundles of widgets you want. I can’t quite picture how this interfaces with your tools, but as your tool loops through its input, record their keys into some kind of orchestration dictionary in Session State.

Alternatively, you could use a naming convention and access keys by prefix. This could be inefficient depending on the case.

I have ended up just storing the keys in the session state under the container’s key as a name. It worked out and I was able to get things working. It would be a good default behavior though, so containers could be used as such in code as well as layout.

You could file a feature request on GitHub. The PMs and devs love to know about specific use cases to go with feature requests.