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

Refactor vega-related charting #8595

Merged
merged 23 commits into from
May 3, 2024
Merged

Conversation

LukasMasuch
Copy link
Collaborator

@LukasMasuch LukasMasuch commented May 2, 2024

Describe your changes

This PR refactors/restructures our backend logic that handles all vega-related charting: st.vega_lite_chart, st.altair_chart, st.line_chart, st.area_chart, st.bar_chart, st.scatter_chart.

  • The built-in charting logic is migrated to built_in_chart_utils
  • All commands are migrated to vega_charts and refactored to reuse more shared logic.
  • This also applies some refactoring to how we handle adding add_rows metadata by just relaying on the available metadata instead of checking for the delta type.

Testing Plan

  • Updated unit tests
  • No big logical changes.

Contribution License Agreement

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

lib/streamlit/delta_generator.py Fixed Show fixed Hide fixed
lib/streamlit/delta_generator.py Fixed Show fixed Hide fixed
lib/streamlit/elements/vega_charts.py Fixed Show fixed Hide fixed
@LukasMasuch LukasMasuch marked this pull request as ready for review May 2, 2024 17:59

return self.dg._enqueue(
"arrow_area_chart", proto, add_rows_metadata=add_rows_metadata
return self._altair_chart(
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: it might be a good idea to leave a small comment about how Vega and Altair belong together, because the class is called VegaChartsMixin but then here we return an altair_chart. This could be confusing to people who do not know about these projects

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a docstring comment to the VegaChartsMixin class to explain how the commands are related 👍

out_data: Data

# For some delta types we have to reshape the data structure
# For built-in chart commands we have to reshape the data structure
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: if this function is only relevant for chart commands but not generally for everything that uses arrow / add-row functionality as the comment says, I suggest to rename the function to sth. like _prep_chart_data_for_add_rows and perhaps move it to the new built_in_chart_utils.py

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We cannot easily fully move this method since its used in all add row cases, but I extracted the chart specific logic to built_in_charts_utils.

Copy link
Collaborator

Choose a reason for hiding this comment

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

alrighty, but is this function only relevant for chart types or in general?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The migrated part is only relevant for built-in charts, but _prep_data_for_add_rows is called in all add row cases.

color_column: str | None,
size_column: str | None,
) -> tuple[pd.DataFrame, str | None, str | None]:
"""If multiple columns are set for y, melt the dataframe into long format."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: could this comment be extended with an example? I am not sure what it means to melt a dataframe into long format

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added more info to the comment

Copy link
Collaborator

@raethlein raethlein left a comment

Choose a reason for hiding this comment

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

Thanks for doing the refactoring 🙌 mainly left nits, but LGTM

@LukasMasuch LukasMasuch merged commit 67d0b04 into develop May 3, 2024
34 checks passed
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

2 participants