Skip to content

Conversation

@vdonato
Copy link
Collaborator

@vdonato vdonato commented Mar 20, 2021

There's a bug that I introduced when adding the theme hashing code that can be
reproduced in two ways.

Way 1 (which is really easy to run into.. not sure how this slipped by 😞):

  1. Use the theme picker to pick some new theme colors.
  2. Refresh the page.
  3. Wonder why the theme didn't revert back to what it was previously and notice that
    the theme name in the theme selector is empty.

Way 2:

  1. Define a custom theme in a config file.
  2. Point your browser at an app and have it load the custom theme by default.
  3. Close the browser tab.
  4. Remove the theme section from the config file.
  5. Load the app again and notice that the custom theme is still applied and the
    theme name in the theme selector is empty.

The root cause of both of these bugs is that we're currently not differentiating
between the theme hash being unset because the app just loaded and it being
a theme hash corresponding to a null theme input. This means when a browser
first loads the app and any custom theme should be removed (because we don't
get one from the server), we don't actually do so.

@vdonato vdonato requested a review from a team March 20, 2021 03:16
Copy link
Contributor

@tconkling tconkling left a comment

Choose a reason for hiding this comment

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

Is there a simple test that fails on the current behavior and passes with this change?

this.handleSessionStateChanged(initialize.sessionState)
}

createThemeHash = (themeInput: CustomThemeConfig): string | null => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is null still a valid return value from this function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope, changed the return value type to just be string here

Comment on lines 583 to 585
// Differentiate between the case where this.state.themeHash has yet to
// be computed (so this.state.themeHash === null) and the case where we
// received no custom theme from the server.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment and function is confusing to me. I might just be lacking context, so take this with a grain of salt, but here's how (I think) this could be made clearer:

  • Change the function's return value to : string. That makes it clear that a non-null, non-undefined string must be returned. (I think this is already the case?)
  • Change this.state.themeHash's type to themeHash?: string. An undefined themeHash has a clear meaning: it's simply not defined yet. this.state.themeHash will never be null.
  • Change this comment to say something like: "If themeInput is null, then the app has no custom theme. We use a hardcoded string literal for the hash in this case."
  • Nit: "no_theme_input" isn't particularly descriptive. What about "undefined_theme_hash" as a string, instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! I agree that changing the type of this.state.themeHash to be optional (and making changes for your other suggestions) does make things more clear here.

Copy link
Collaborator Author

@vdonato vdonato left a comment

Choose a reason for hiding this comment

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

Is there a simple test that fails on the current behavior and passes with this change?

The test on App.test.tsx line 389 serves that purpose after the line setting themeHash to "hash_for_undefined_custom_theme" was added.

Then the tests where the wrapper.setState({ themeHash: "someHash" }) lines were removed cover the cases where a custom theme option is being removed but we haven't processed its removal yet

this.handleSessionStateChanged(initialize.sessionState)
}

createThemeHash = (themeInput: CustomThemeConfig): string | null => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nope, changed the return value type to just be string here

Comment on lines 583 to 585
// Differentiate between the case where this.state.themeHash has yet to
// be computed (so this.state.themeHash === null) and the case where we
// received no custom theme from the server.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! I agree that changing the type of this.state.themeHash to be optional (and making changes for your other suggestions) does make things more clear here.

@vdonato vdonato requested a review from tconkling March 24, 2021 01:39
Copy link
Contributor

@tconkling tconkling left a comment

Choose a reason for hiding this comment

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

Looks great!

@vdonato vdonato merged commit 7d9171d into streamlit:develop Mar 24, 2021
tconkling added a commit that referenced this pull request Mar 24, 2021
# By Vincent Donato (3) and Nikitas Rontsis (1)
# Via GitHub
* develop:
  Have theme hashes differentiate between being unset and the hash for null (#2993)
  Improve module reloading (#3001)
  Add tests to improve coverage around config options set via flag (#2991)
  Add snippets about CSS vars and minimum streamlit-component-lib versions (#3008)

# Conflicts:
#	frontend/src/App.tsx
@vdonato vdonato deleted the theme-hash-fix branch March 26, 2021 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants