Skip to content

Conversation

@kaydotdev
Copy link
Contributor

@kaydotdev kaydotdev commented May 7, 2023

📚 Context

Please describe the project or issue background here

  • What kind of change does this PR introduce?

    • Bugfix
    • Feature
    • Refactoring
    • Other, please describe:

🧠 Description of Changes

  • Add bullet points summarizing your changes here

    • This is a breaking API change
    • This is a visible (user-facing) change

Current:

streamlit.mp4

🧪 Testing Done

  • Screenshots included
  • Added/Updated unit tests

🌐 References

Does this depend on other work, documents, or tickets?


Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@kaydotdev kaydotdev marked this pull request as draft May 7, 2023 23:03
@kaydotdev kaydotdev changed the title 3605/disable warning session state key Add a config option to disable warning for setting both a widget default and its key in session_state May 8, 2023
@kaydotdev kaydotdev marked this pull request as ready for review May 8, 2023 22:36
@vdonato vdonato added the security-assessment-completed Security assessment has been completed for PR label May 18, 2023
@sfc-gh-jcarroll
Copy link
Contributor

Thanks for submitting this! BTW, what was the use case for you where you needed this support to intentionally set both the default and key? Was discussing with @vdonato and we couldn't remember since it had been a while since the original issue was filed.

But assuming we have a clear use case, seems like a good way to handle it :)

@kaydotdev
Copy link
Contributor Author

kaydotdev commented May 19, 2023

@sfc-gh-jcarroll, a useful case may be when a component state affects other components, like in this example:

import streamlit as st
import matplotlib.pyplot as plt
import numpy as np


st.header("Normal distribution")

col_plot, col_settings = st.columns(2)
MU_DEFAULT, SIGMA_DEFAULT, SAMPLES_DEFAULT = 0.0, 1.0, 1000

if st.button("Set default"):
    st.session_state.mu = MU_DEFAULT
    st.session_state.sigma = SIGMA_DEFAULT
    st.session_state.samples = SAMPLES_DEFAULT

if st.button("Max samples"):
    st.session_state.samples = 100_000

with col_settings:
    st.slider("Distribution mean", -100.0, 100.0, MU_DEFAULT, key="mu")
    st.slider("Distribution standard deviation", 0.01, 100.0, SIGMA_DEFAULT, key="sigma")
    st.number_input("Distribution samples", 0, 100_000, SAMPLES_DEFAULT, key="samples")

dist = np.random.normal(st.session_state.mu,
                        st.session_state.sigma,
                        st.session_state.samples)

with col_plot:
    fig, ax = plt.subplots()

    counts, bins, _ = ax.hist(dist, 30, density=True)
    ax.plot(bins, 1.0 / (st.session_state.sigma * np.sqrt(2.0 * np.pi)) * np.exp( - (bins - st.session_state.mu) ** 2.0 / (2.0 * st.session_state.sigma ** 2.0)), linewidth=2, color='r')
    st.pyplot(fig)

image

Also, I think another case may be a multi-paged application having a dedicated Settings page, with the global session state params, that are used in other pages.

@kajarenc
Copy link
Collaborator

Thanks, @antonace!

@kajarenc kajarenc merged commit 41674df into streamlit:develop May 21, 2023
@kaydotdev
Copy link
Contributor Author

@kajarenc, awesome! Thank you for approving and adding these changes to the project!

tconkling added a commit to tconkling/streamlit that referenced this pull request May 30, 2023
* develop: (22 commits)
  enzyme -> react-testing-library (Countdown, Modal, ProgressBar) (streamlit#6744)
  Remove console.log (streamlit#6753)
  Update `st.data_editor` and `st.dataframe` docstrings (streamlit#6752)
  Update `st.data_editor` session state format (streamlit#6711)
  Cypress flaky test fixes (streamlit#6743)
  Document integer size limit for number_input and slider (streamlit#6724)
  Change default theme hash on app init (streamlit#6729)
  Use .genericFonts instead of .fonts to fix bug with theme postMessage. (streamlit#6732)
  Improve startup performance by lazy loading some dependencies (streamlit#6531)
  Add support for Altair 5 (streamlit#6618)
  Update modals (streamlit#6688)
  Improve docstrings for `ttl` and `max_entries` (streamlit#6733)
  Improve `st.columns` docstring (streamlit#6727)
  Removed orphan line in dataframe docstring (streamlit#6734)
  Fix useIsOverflowing dependency array (streamlit#6731)
  Remove experimental from data editor (streamlit#6712)
  Clarify set_page_config docstring and exception message (streamlit#6594)
  Add a config option to disable warning for setting both a widget default and its key in session_state (streamlit#6640)
  Add column configuration API for `st.dataframe` and `st.data_editor` (streamlit#6598)
  Migrate datetime column formatting from date-fns to momentJS (streamlit#6702)
  ...
@jrieke jrieke added the change:feature PR contains new feature or enhancement implementation label Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a config option to disable warning for setting both a widget default and its key in session_state

5 participants