Skip to content

Conversation

@jose-mindwayai
Copy link
Contributor

@jose-mindwayai jose-mindwayai commented Aug 7, 2025

Describe your changes

  1. Extended the initial_sidebar_state in st.set_page_config() parameter to accept an integer value for setting initial sidebar width in pixels.

The initial_sidebar_state parameter in st.set_page_config() now accepts:

  • "auto" (default) - Auto-sizing behavior
  • "expanded" - Sidebar expanded on load
  • "collapsed" - Sidebar collapsed on load
  • int - Set initial sidebar width in pixels and uses the "auto" behaviour
  1. Changed the default sidebar width from 256px to 300px

GitHub Issue Link (if applicable)

Ability to Set Default Sidebar Width #11980

Testing Plan

I did some manual testing and added Unit Tests for the sidebar width validation.


Contribution License Agreement

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

@jose-mindwayai jose-mindwayai requested a review from a team as a code owner August 7, 2025 18:50
@snyk-io
Copy link
Contributor

snyk-io bot commented Aug 7, 2025

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@sfc-gh-lwilby sfc-gh-lwilby added the status:needs-product-approval PR requires product approval before merging label Aug 7, 2025
@sfc-gh-lwilby
Copy link
Collaborator

Hi @jose-mindwayai ,

Thanks for submitting this PR! At a first glance, it looks good.

Before we review/merge PRs we need product approval that the feature is something we want to add. @jrieke is out this week but he will take a look when he gets back.

@jose-mindwayai
Copy link
Contributor Author

Hi @sfc-gh-lwilby, thanks for the update! Sounds good 👍

@sfc-gh-lmasuch sfc-gh-lmasuch added change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Aug 8, 2025
@jrieke
Copy link
Collaborator

jrieke commented Aug 16, 2025

Hey @jose-mindwayai! Thanks for the contribution. From your issue, it seems like the only problem you have is that the new sidebar width is too small for you, but you were happy with the old one? In that case, I'm thinking whether we should just revert to the old width... I'm not a huge fan of introducing a theme option for this, since I think most people will be fine with the default width (we just need to make sure it's a good default!).

Can you by any chance share some screenshots of how your app/sidebar looks with the old and new widths?

@jose-mindwayai
Copy link
Contributor Author

Hi @jrieke, thanks for getting back to me! 🙂 Yes, you’re right — the main issue I ran into is that the new sidebar width feels a bit too narrow compared to the old one. I didn’t have any problems with the previous width, so reverting would definitely solve it for my use case. That said, I think having an option may be useful since sidebar content can vary a lot between applications.

This is a screnshot of the old preset which was set at 336px.
336px

This is a screnshot of the new preset which is set at 256px. I think the sidebar contents feel a little squished because the filter controls don’t have enough horizontal breathing room. Text like “Avg. session time” wraps more tightly and some labels feel crowded against the edges.

256px

@jrieke
Copy link
Collaborator

jrieke commented Aug 29, 2025

Hey, sorry for the delay! We're discussing internally whether we should revert that back to the old sidebar width now. As I mentioned above, I'd much prefer this over adding a config option/parameter for it.

@jose-mindwayai
Copy link
Contributor Author

No worries! That makes sense 👍

@jose-mindwayai
Copy link
Contributor Author

Hi @jrieke have you got any updates? :)

@jrieke
Copy link
Collaborator

jrieke commented Sep 23, 2025

Hey @jose-mindwayai! So we are fine with changing the sidebar width again. As I said before, I think I agree that the 256px width was a bit too narrow.

I've been just playing around and I was thinking maybe we should do 300px by default, that's a nice in-between value that's a bit smaller than the old one but not too narrow. Then your sidebar above would show up like this on a 15 inch notebook, what do you think?

Screenshot 2025-09-23 at 02 41 18@2x

I think I'd be in principle even fine with having a parameter for the default width. The question is just where to put it. Theming feels a bit awkward since this isn't really part of a "theme" to make your app follow a different design system.

One pretty clever thing we could do is fold this into the initial_sidebar_state parameter of st.set_page_config. So that in addition to the existing values for that parameter ("auto"|"expanded"|"collapsed"|None), you can pass an integer, which then sets the initial sidebar width in pixels. This way, we wouldn't need to add a parameter for a pretty niche feature. The only slight downside is that you couldn't hide the sidebar AND set a default width – but I think that's probably a tiny edge case anyway. What do you think about that solution?

@jose-mindwayai
Copy link
Contributor Author

Hey @jrieke really cool that you took the time to reproduce the issue! 🙂

I agree that 300px seems like a good default for my use case, definitely a nice middle ground. That said, I also think it would be great to give users the option to customize the sidebar width.
I also agree with your suggestion that st.set_page_config is a better place for this than the theming options 👍

Should I try implementing it that way?

@jrieke
Copy link
Collaborator

jrieke commented Sep 23, 2025

Yup, if you have the time for it, please go ahead! And you can also include the change to 300px in the same PR. Let me know when I should have a look :)

@jose-mindwayai
Copy link
Contributor Author

Hey @jrieke I think this should work now 👍 Let me know what you think :)

@jrieke
Copy link
Collaborator

jrieke commented Oct 1, 2025

Awesome! On a conference at the moment, so might take a bit, but trying to have a look next week!

@jose-mindwayai
Copy link
Contributor Author

@jrieke I've also added the new snapshots now :)

@jose-mindwayai
Copy link
Contributor Author

Hey @jrieke sorry to bug you again, just wondering if you got any updates?

@jrieke
Copy link
Collaborator

jrieke commented Oct 20, 2025

Hey, no worries, thanks for the ping, we had a lot going on! This seems to work fine, the only issue I found is that if you put in a really large value (e.g. initial_sidebar_state=5000), it seems to go back to the default 300 px. I think in that case it should have the maximum allowed width (600 px).

Lmk as soon as that is fixed and then I'll get our engs to have a look!

@jose-mindwayai
Copy link
Contributor Author

Hey, no worries, thanks for the ping, we had a lot going on! This seems to work fine, the only issue I found is that if you put in a really large value (e.g. initial_sidebar_state=5000), it seems to go back to the default 300 px. I think in that case it should have the maximum allowed width (600 px).

Lmk as soon as that is fixed and then I'll get our engs to have a look!

Thanks @jrieke I hadn't noticed that 😅 I have now limited the width between 200 and 600px.

Everything seems to be working now!

@275RR
Copy link

275RR commented Oct 26, 2025

I am also interested in setting the width of the sidebar because I want to to create a fixed-width bar to display icons only. Does the min-width need to be clamped at 200? For an icon bar, I might want something like 64 or 128.

Random image for reference:
image

Comment on lines 105 to 112
const [sidebarWidth, setSidebarWidth] = useState<string>(() => {
// Use initialSidebarWidth if available, otherwise fall back to cached or default
if (notNullOrUndefined(initialSidebarWidth)) {
const clampedWidth = clampSidebarWidth(initialSidebarWidth)
return cachedSidebarWidth || clampedWidth.toString()
}
return cachedSidebarWidth || DEFAULT_WIDTH
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cached sidebar width from localStorage is not being clamped, which could result in invalid width values being applied on initial load. If a user manually edits localStorage or if the cached value predates the min/max constraints, the sidebar could render with an invalid width (e.g., 1000px which exceeds the 600px maximum).

The fix should clamp the cached width before using it:

const [sidebarWidth, setSidebarWidth] = useState<string>(() => {
  const getCachedWidth = () => {
    if (cachedSidebarWidth) {
      const cached = parseInt(cachedSidebarWidth, 10)
      return isNaN(cached) ? null : clampSidebarWidth(cached).toString()
    }
    return null
  }
  
  const clampedCached = getCachedWidth()
  
  if (clampedCached) {
    return clampedCached
  }
  
  if (notNullOrUndefined(initialSidebarWidth)) {
    return clampSidebarWidth(initialSidebarWidth).toString()
  }
  
  return DEFAULT_WIDTH
})
Suggested change
const [sidebarWidth, setSidebarWidth] = useState<string>(() => {
// Use initialSidebarWidth if available, otherwise fall back to cached or default
if (notNullOrUndefined(initialSidebarWidth)) {
const clampedWidth = clampSidebarWidth(initialSidebarWidth)
return cachedSidebarWidth || clampedWidth.toString()
}
return cachedSidebarWidth || DEFAULT_WIDTH
})
const [sidebarWidth, setSidebarWidth] = useState<string>(() => {
const getCachedWidth = () => {
if (cachedSidebarWidth) {
const cached = parseInt(cachedSidebarWidth, 10)
return isNaN(cached) ? null : clampSidebarWidth(cached).toString()
}
return null
}
const clampedCached = getCachedWidth()
if (clampedCached) {
return clampedCached
}
if (notNullOrUndefined(initialSidebarWidth)) {
return clampSidebarWidth(initialSidebarWidth).toString()
}
return DEFAULT_WIDTH
})

Spotted by Graphite Agent

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jose-mindwayai this seems like a good idea if you can address this prior to merging.

@sfc-gh-lwilby
Copy link
Collaborator

@jose-mindwayai heads up there were some merge conflicts in your branch so I have rebased on develop for you and force pushed the changes.

@sfc-gh-lwilby
Copy link
Collaborator

@jose-mindwayai this PR looks good to me! The only thing is this graphite review comment above, this seems like a tood thing to address before merging because it could perhaps lead to some user experience issues. I've completed the rebase and fixed up the branch for you including a force push after rebasing so heads up on that.

@jose-mindwayai
Copy link
Contributor Author

Hi @sfc-gh-lwilby thank you very much for the help :)

I've added graphite's suggestion and created some test cases for that as well 👍

Copy link
Collaborator

@sfc-gh-lwilby sfc-gh-lwilby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thank you @jose-mindwayai

@jose-mindwayai
Copy link
Contributor Author

Great, thanks for the review @sfc-gh-lwilby!

@sfc-gh-lwilby sfc-gh-lwilby merged commit 16cc5c2 into streamlit:develop Dec 2, 2025
40 checks passed
@jose-mindwayai jose-mindwayai deleted the set-sidebar-width branch December 4, 2025 07:57
@jrieke
Copy link
Collaborator

jrieke commented Dec 10, 2025

Wohoo, thanks for getting this across the finish line, @jose-mindwayai! 🥳 Would love to send you some swag as a little thank you, just fill out this form and we'll get it on the way: https://forms.gle/RZCUTKCrg7aHyqH78

@jose-mindwayai
Copy link
Contributor Author

Thanks a lot @jrieke, I really appreciate it 😄
Just filled out the form. Excited for the swag! 🙌

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 impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR status:product-approved Community PR is approved by product team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants