Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache JSON when using pydeck local data #7113

Merged
merged 16 commits into from Aug 9, 2023
Merged

Cache JSON when using pydeck local data #7113

merged 16 commits into from Aug 9, 2023

Conversation

willhuang1997
Copy link
Collaborator

@willhuang1997 willhuang1997 commented Aug 3, 2023

Describe your changes

  • add a protobuf key named element id which is a hash of the pydeck json

  • add logic to not parse the json again where the json is stored in state

    • if the hash string is different, then parse it again
    • if it’s not, use the cached json
  • add fullScreen and theme to state

    • if these change, we need to parse the json again to rerender the new map
  • these changes make the loading slightly longer when rerunning or running the script; however, the experience is significantly better so it's worth to do this.

GitHub Issue Link (if applicable)

#5532

Testing Plan

  • Explanation of why no additional tests are needed
  • Unit Tests (JS and/or Python)
  • will need to add these
  • E2E Tests
  • very difficult to add these so will not be adding these.
  • Any manual testing needed?
  • yes, i manually tested the test case within the issue 5532. I will grab other use cases from pydeck and make sure they're responsive.

This branch:
https://github.com/streamlit/streamlit/assets/16749069/130d21ca-efe4-437b-aa6e-fb1a04c6805b

Develop:
https://github.com/streamlit/streamlit/assets/16749069/275b4e00-4f1d-444e-bf69-f84040c9b86f


Contribution License Agreement

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


const getProps = (
elementProps: Partial<DeckGlJsonChartProto> = {},
initialViewStateProps: Record<string, unknown> = {}
): PropsWithHeight => {
const json = {
initialViewState: {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

just moved this to top of file so I can reuse later in the tests

@willhuang1997 willhuang1997 marked this pull request as ready for review August 7, 2023 18:10

const isFullScreen = Boolean(height)

// Only parse JSON when not transitioning to/from fullscreen, the element id changes, or theme changes
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

maybe i should remove this comment since it's not necessary?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could also add one more sentence here why we need to reparse with these changes

if (
element.id !== id ||
state.isFullScreen !== currFullScreen ||
state.isLightTheme !== hasLightBackgroundColor(theme)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to reparse this when layout changes? Aren't we always adapting the mapStyle below anyways based on the theme?

Copy link
Collaborator

@LukasMasuch LukasMasuch left a comment

Choose a reason for hiding this comment

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

LGTM if the comments above are checked 👍 This component could probably benefit from a rewrite to functional API sometime later since this is a perfect usecase for useState with dependencies.

@sfc-gh-wihuang
Copy link
Contributor

LGTM if the comments above are checked 👍 This component could probably benefit from a rewrite to functional API sometime later since this is a perfect usecase for useState with dependencies.

Will fix them. Currently in progress! thanks!

@LukasMasuch
Copy link
Collaborator

LukasMasuch commented Aug 7, 2023

Ahh, and maybe this would be a good opportunity to also rewrite the unit test of deckgl component to use RTL instead of enzyme (shallow).

@sfc-gh-wihuang
Copy link
Contributor

Ahh, and maybe this would be a good opportunity to also rewrite the unit test of deckgl component to use RTL instead of enzyme (shallow).

Sounds good. I'll add that to things to do this week

deckGL.prop("onViewStateChange")({
viewState: { pitch: 5, zoom: 5 },
})
const state = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed this test to not involve calling DeckGl methods because that seems odd. So I just changed this test to test our getDerivedStateFromProps function.

Testing the changing of pydeck with unit testing and RTL is turning out to be a little too difficult as we can click the zoom button but it's not immediately obvious how to check the map changed. That should likely be tested with e2e tests and thus changing this test seems like a reasonable thing from my perspective.

it("should render tooltip", () => {
const props = getProps({
tooltip: `{"html": "<b>Elevation Value:</b> {elevationValue}", "style": {"color": "white"}}`,
describe("createTooltip", () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed this test to not involve calling DeckGl methods because that seems odd. So I just changed this test to test our createTooltip function.

Testing the changing of pydeck with unit testing and RTL is turning out to be a little too difficult as we can hover but it's not immediately obvious how to check the tooltip existing. That should likely be tested with e2e tests and thus changing this test seems like a reasonable thing from my perspective.

@willhuang1997 willhuang1997 merged commit 1ef8b83 into develop Aug 9, 2023
49 checks passed
@vdonato vdonato deleted the fix/5532 branch August 19, 2023 01:54
eric-skydio pushed a commit to eric-skydio/streamlit that referenced this pull request Dec 20, 2023
<!--
⚠️ BEFORE CONTRIBUTING PLEASE READ OUR CONTRIBUTING GUIDELINES!
https://github.com/streamlit/streamlit/wiki/Contributing
-->

## Describe your changes
- add a protobuf key named element id which is a hash of the pydeck json
- add logic to not parse the json again where the json is stored in state
  - if the hash string is different, then parse it again
  - if it’s not, use the cached json
- add fullScreen and theme to state
  - if these change, we need to parse the json again to rerender the new map
  
- these changes make the loading slightly longer when rerunning or running the script; however, the experience is significantly better so it's worth to do this.
## GitHub Issue Link (if applicable)
streamlit#5532
## Testing Plan

- Explanation of why no additional tests are needed
- Unit Tests (JS and/or Python)
- will need to add these
- E2E Tests
- very difficult to add these so will not be adding these.
- Any manual testing needed?
- yes, i manually tested the test case within the issue 5532. I will grab other use cases from pydeck and make sure they're responsive.

This branch:
https://github.com/streamlit/streamlit/assets/16749069/130d21ca-efe4-437b-aa6e-fb1a04c6805b


Develop:
https://github.com/streamlit/streamlit/assets/16749069/275b4e00-4f1d-444e-bf69-f84040c9b86f



---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Mar 22, 2024
<!--
⚠️ BEFORE CONTRIBUTING PLEASE READ OUR CONTRIBUTING GUIDELINES!
https://github.com/streamlit/streamlit/wiki/Contributing
-->

## Describe your changes
- add a protobuf key named element id which is a hash of the pydeck json
- add logic to not parse the json again where the json is stored in state
  - if the hash string is different, then parse it again
  - if it’s not, use the cached json
- add fullScreen and theme to state
  - if these change, we need to parse the json again to rerender the new map
  
- these changes make the loading slightly longer when rerunning or running the script; however, the experience is significantly better so it's worth to do this.
## GitHub Issue Link (if applicable)
streamlit#5532
## Testing Plan

- Explanation of why no additional tests are needed
- Unit Tests (JS and/or Python)
- will need to add these
- E2E Tests
- very difficult to add these so will not be adding these.
- Any manual testing needed?
- yes, i manually tested the test case within the issue 5532. I will grab other use cases from pydeck and make sure they're responsive.

This branch:
https://github.com/streamlit/streamlit/assets/16749069/130d21ca-efe4-437b-aa6e-fb1a04c6805b


Develop:
https://github.com/streamlit/streamlit/assets/16749069/275b4e00-4f1d-444e-bf69-f84040c9b86f



---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Apr 16, 2024
<!--
⚠️ BEFORE CONTRIBUTING PLEASE READ OUR CONTRIBUTING GUIDELINES!
https://github.com/streamlit/streamlit/wiki/Contributing
-->

## Describe your changes
- add a protobuf key named element id which is a hash of the pydeck json
- add logic to not parse the json again where the json is stored in state
  - if the hash string is different, then parse it again
  - if it’s not, use the cached json
- add fullScreen and theme to state
  - if these change, we need to parse the json again to rerender the new map
  
- these changes make the loading slightly longer when rerunning or running the script; however, the experience is significantly better so it's worth to do this.
## GitHub Issue Link (if applicable)
streamlit#5532
## Testing Plan

- Explanation of why no additional tests are needed
- Unit Tests (JS and/or Python)
- will need to add these
- E2E Tests
- very difficult to add these so will not be adding these.
- Any manual testing needed?
- yes, i manually tested the test case within the issue 5532. I will grab other use cases from pydeck and make sure they're responsive.

This branch:
https://github.com/streamlit/streamlit/assets/16749069/130d21ca-efe4-437b-aa6e-fb1a04c6805b


Develop:
https://github.com/streamlit/streamlit/assets/16749069/275b4e00-4f1d-444e-bf69-f84040c9b86f



---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants