i have a strreamlit streamlit modal window [code] produced html code [code] I want to…
- User
i have a strreamlit streamlit modal window
from streamlit_modal import Modal import streamlit.components.v1 as components modal = Modal( "Demo Modal", key="demo-modal", # Optional padding=20, # default value max_width=744 # default value ) open_modal = st.button("Open") if open_modal: modal.open() if modal.is_open(): with modal.container(): st.write("Text goes here") st.write("Some fancy text") value = st.checkbox("Check me") st.write(f"Checkbox checked: {value}")produced html code
<div width="744" data-testid="stVerticalBlock" class="st-emotion-cache-l6wp7i e1f1d6gn2" style="background-color: rgb(255, 255, 255);"> <div data-testid="stVerticalBlockBorderWrapper" data-test-scroll-behavior="normal" class="st-emotion-cache-0 e1f1d6gn0"> <div class="st-emotion-cache-1wmy9hl e1f1d6gn1"> <div width="738" data-testid="stVerticalBlock" class="st-emotion-cache-11teiw1 e1f1d6gn2"> <div data-testid="stHorizontalBlock" class="st-emotion-cache-ocqkz7 e1f1d6gn5"> <div data-testid="column" class="st-emotion-cache-1jxxjay e1f1d6gn3"> <div data-testid="stVerticalBlockBorderWrapper" data-test-scroll-behavior="normal" class="st-emotion-cache-0 e1f1d6gn0"> <div class="st-emotion-cache-1wmy9hl e1f1d6gn1"> <div width="656.203125" data-testid="stVerticalBlock" class="st-emotion-cache-1ii0q45 e1f1d6gn2"> <div data-stale="false" width="656.203125" class="element-container st-emotion-cache-16lp00d e1f1d6gn4" data-testid="element-container"> <div class="stHeading" data-testid="stHeading" style="width: 656.203px;"> <div data-testid="stMarkdownContainer" class="st-emotion-cache-1rsyhoq e1nzilvr5" style="width: 656.203px;"> <div data-testid="stHeadingWithActionElements" class="st-emotion-cache-pgf13w e1nzilvr3"> <h2 id="demo-modal">Demo Modal <span data-testid="stHeaderActionElements" class="st-emotion-cache-gi0tri e1nzilvr2"> <a href="#demo-modal" class="st-emotion-cache-yinll1 e1nzilvr4"> <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path> <line x1="8" y1="12" x2="16" y2="12"></line> </svg> </a> </span> </h2> </div> </div> </div> </div> </div> </div> </div> </div> <div data-testid="column" class="st-emotion-cache-ytkq5y e1f1d6gn3"> <div data-testid="stVerticalBlockBorderWrapper" data-test-scroll-behavior="normal" class="st-emotion-cache-0 e1f1d6gn0"> <div class="st-emotion-cache-1wmy9hl e1f1d6gn1"> <div width="65.8125" data-testid="stVerticalBlock" class="st-emotion-cache-5407hu e1f1d6gn2"> <div data-stale="false" width="65.8125" class="element-container st-emotion-cache-16lg4gj e1f1d6gn4" data-testid="element-container"> <div class="stButton" data-testid="stButton" style="width: 65.8125px;"> <button kind="secondary" data-testid="stBaseButton-secondary" class="st-emotion-cache-1695wks ef3psqc13"> <div data-testid="stMarkdownContainer" class="st-emotion-cache-12h5x7g e1nzilvr5"> <p>X</p> </div> </button> </div> </div> </div> </div> </div> </div> </div> <div data-stale="false" width="738" class="element-container st-emotion-cache-126puhk e1f1d6gn4" data-testid="element-container"> <div class="stMarkdown" data-testid="stMarkdown" style="width: 738px;"> <div data-testid="stMarkdownContainer" class="st-emotion-cache-1rsyhoq e1nzilvr5"> <hr> </div> </div> </div> <div data-stale="false" width="738" class="element-container st-emotion-cache-126puhk e1f1d6gn4" data-testid="element-container"> <div class="stMarkdown" data-testid="stMarkdown" style="width: 738px;"> <div data-testid="stMarkdownContainer" class="st-emotion-cache-1rsyhoq e1nzilvr5"> <p>Text goes here</p> </div> </div> </div> <div data-stale="false" width="738" class="element-container st-emotion-cache-126puhk e1f1d6gn4" data-testid="element-container"> <div class="stMarkdown" data-testid="stMarkdown" style="width: 738px;"> <div data-testid="stMarkdownContainer" class="st-emotion-cache-1rsyhoq e1nzilvr5"> <p>Some fancy text</p> </div> </div> </div> <div data-stale="false" width="738" class="element-container st-emotion-cache-126puhk e1f1d6gn4" data-testid="element-container"> <div class="stCheckbox st-emotion-cache-1d4vg55 edwcd611" data-testid="stCheckbox" width="738"> <label data-baseweb="checkbox" class="st-d5 st-af st-d6 st-d7 st-d8 st-cg st-cf st-d9 st-da"> <span class="st-db st-ar st-as st-dc st-dd st-de st-df st-dg st-ae st-am st-an st-ao st-ap st-ai st-aj st-ak st-al st-dh st-di st-dj st-dk st-dl st-dm st-dn st-do st-dp st-cd st-dq st-dr st-ds st-dt st-du st-dv st-cg st-cr st-dw st-dx"></span> <input aria-label="Check me" aria-checked="false" type="checkbox" class="st-dy st-dz st-cw st-ag st-bq st-e0 st-cs" value=""> <div class="st-dq st-e1 st-bc st-ax st-ay st-az st-e2 st-b1 st-b2 st-bz"> <div visibility="0" data-testid="stWidgetLabel" class="st-emotion-cache-y4bq5x edwcd610"> <div data-testid="stMarkdownContainer" class="st-emotion-cache-12h5x7g e1nzilvr5"> <p>Check me</p> </div> </div> </div> </label> </div> </div> <div data-stale="false" width="738" class="element-container st-emotion-cache-126puhk e1f1d6gn4" data-testid="element-container"> <div class="stMarkdown" data-testid="stMarkdown" style="width: 738px;"> <div data-testid="stMarkdownContainer" class="st-emotion-cache-1rsyhoq e1nzilvr5"> <p>Checkbox checked: False</p> </div> </div> </div> </div> </div> </div> </div> - Assistant
To customize the font size of the title and reduce the space between the title and the line in your Streamlit modal, you can use HTML and CSS styling. You can add custom CSS directly into your Streamlit app using the
st.markdownfunction with theunsafe_allow_html=Trueparameter.Below is the modified code that includes custom CSS for adjusting the font size of the title and the margin between the title and the line:
- User
remove the open button from streamlit code
- Assistant
If you want to remove the button that opens the modal and instead open the modal automatically when the app is run, you can simply call
modal.open()without the conditional check for a button press. Here’s how your modified code would look:from streamlit_modal import Modal import streamlit as st # Custom CSS to style the modal title and spacing custom_css = """ <style> .modal-title { font-size: 20px; /* Adjust font size here */ margin-bottom: 5px; /* Reduce space between the title and the line */ } .stMarkdown hr { margin-top: 5px; /* Adjust space above the line if needed */ } </style> """ # Display custom CSS st.markdown(custom_css, unsafe_allow_html=True) modal = Modal( "Demo Modal", key="demo-modal", # Optional padding=20, # default value max_width=744 # default value ) # Open the modal automatically modal.open() if modal.is_open(): with modal.container(): st.markdown('<div class="modal-title">Demo Modal</div>', unsafe_allow_html=True) # Custom title with class st.markdown("---") # Horizontal line st.write("Text goes here") st.write("Some fancy text") value = st.checkbox("Check me") st.write(f"Checkbox checked: {value}") - User
it's running infinitevely
- Assistant
When you call
modal.open()directly without any conditional checks, it keeps reopening the modal on every rerun of the Streamlit app (due to Streamlit's reactivity model), which can lead to an infinite loop of the modal being opened.To resolve this issue, you can use a session state variable to control when the modal should be opened. Here's how you can modify the code to achieve this: