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
Add color support to charts, and refactor things a bit. #7022
Add color support to charts, and refactor things a bit. #7022
Conversation
@@ -305,14 +303,7 @@ def generate_chart(chart_type, data, width: int = 0, height: int = 0): | |||
data = {"": []} | |||
|
|||
if isinstance(data, pa.Table): | |||
raise errors.StreamlitAPIException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this to its own error type. See bottom of the new code.
616e778
to
299d047
Compare
299d047
to
de37f07
Compare
lib/streamlit/type_util.py
Outdated
@@ -558,16 +597,16 @@ def convert_anything_to_df( | |||
|
|||
|
|||
@overload | |||
def ensure_iterable(obj: Iterable[V_co]) -> Iterable[V_co]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just me fixing some pre-existing MyPy typing problems that were blocking this PR from passing tests.
…ting the orig DF.
…ill land in another PR.
else: | ||
return _melt_data(df=data, last_index=last_index) | ||
out_data, *_ = prep_data(df, **add_rows_metadata.columns) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Properly handle the data in add_rows()
by calling here the exact same prep_data
step we use in _generate_chart()
MELTED_Y_COLUMN_NAME = MELTED_Y_COLUMN_TITLE + PROTECTION_SUFFIX | ||
MELTED_COLOR_COLUMN_NAME = MELTED_COLOR_COLUMN_TITLE + PROTECTION_SUFFIX | ||
|
||
# Name we use for a column we know doesn't exist in the data, to address a Vega-Lite rendering bug |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a link to this vega-lite bug?
This PR is pretty long so will review again later tonight or tomorrow morning. Also Cypress test (4) is failing and I tried rerunning it. Can you fix that? |
PR ready! I just sent it out for QA. Waiting for results before I can merge. |
… since JSON stringifies keys anyways, but this is cleaner.
* Add color support to charts, and refactor things a bit. * Fix tests and types. * Fix types in type_util * Removin unnecessary DF cloning, and add test to ensure we're not mutating the orig DF. * Fix tests in latest Pandas and Arrow. * Revert st_arrow_add_rows test to avoid too many changes in this PR. Will land in another PR. * Remove snaps that should be regenerated. * Don't show labels when no x or y specified (to match existing behavior). * Update snapshots (need to regenerate) * Fix bug where x axis was sometimes quantitative rather than temporal. * Fix x enc when no x_column. * Fix tests. * Revert to melting data by hand since Snowflake doesn't include Altair 4.2 * Improve error messages. * Fix MyPy types * Update snapshots * Fix empty check * Revert setup.py * Remove dead code. * simple rename * Remove more dead code * Address comments. * Fix snapshots * Fix stupid copy/paste bug >_< * Small cleanup. Always use str() around dataset ids. Should be a no-op since JSON stringifies keys anyways, but this is cleaner.
* Add color support to charts, and refactor things a bit. * Fix tests and types. * Fix types in type_util * Removin unnecessary DF cloning, and add test to ensure we're not mutating the orig DF. * Fix tests in latest Pandas and Arrow. * Revert st_arrow_add_rows test to avoid too many changes in this PR. Will land in another PR. * Remove snaps that should be regenerated. * Don't show labels when no x or y specified (to match existing behavior). * Update snapshots (need to regenerate) * Fix bug where x axis was sometimes quantitative rather than temporal. * Fix x enc when no x_column. * Fix tests. * Revert to melting data by hand since Snowflake doesn't include Altair 4.2 * Improve error messages. * Fix MyPy types * Update snapshots * Fix empty check * Revert setup.py * Remove dead code. * simple rename * Remove more dead code * Address comments. * Fix snapshots * Fix stupid copy/paste bug >_< * Small cleanup. Always use str() around dataset ids. Should be a no-op since JSON stringifies keys anyways, but this is cleaner.
* Add color support to charts, and refactor things a bit. * Fix tests and types. * Fix types in type_util * Removin unnecessary DF cloning, and add test to ensure we're not mutating the orig DF. * Fix tests in latest Pandas and Arrow. * Revert st_arrow_add_rows test to avoid too many changes in this PR. Will land in another PR. * Remove snaps that should be regenerated. * Don't show labels when no x or y specified (to match existing behavior). * Update snapshots (need to regenerate) * Fix bug where x axis was sometimes quantitative rather than temporal. * Fix x enc when no x_column. * Fix tests. * Revert to melting data by hand since Snowflake doesn't include Altair 4.2 * Improve error messages. * Fix MyPy types * Update snapshots * Fix empty check * Revert setup.py * Remove dead code. * simple rename * Remove more dead code * Address comments. * Fix snapshots * Fix stupid copy/paste bug >_< * Small cleanup. Always use str() around dataset ids. Should be a no-op since JSON stringifies keys anyways, but this is cleaner.
Describe your changes
Add support for
color
argument in charts. As part of this work, I also refactored how charts work a bit, in order to prepare the land forst.scatter_chart
, which is coming in a subsequent PR.This is PR 3 in the work to:
(PR 1 was a no-op that added the color_util.py library, which is used in this and the next PRs)
What else this does:
_generate_chart()
, and next is_maybe_melt()
. The good news is the new code is much more readable 😉title
attributes._generate_chart
. This is just the right thing, and may even address some undiscovered bugs.How to review this
The main file you need to understand is
lib/streamlit/elements/arrow_altair.py
, in particular the_generate_chart()
function. It's a bit of a behemoth. Luckily, everything else (especially outside that file) is just supporting code that should be quite straight-forward to understand once you grok that function.GitHub Issue Link (if applicable)
n/a
Testing Plan
N/A - Explanation of why no additional tests are needed
✅ Unit Tests (JS and/or Python)
✅ E2E Tests
Any manual testing needed?
Yes. I'll request this from the QA team soon.
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.