Skip to content

Conversation

@lukasmasuch
Copy link
Collaborator

@lukasmasuch lukasmasuch commented May 3, 2023

📚 Context

Altair 5 was released in a couple of days ago. This PR makes Streamlit compatible with the 5.x version. More specifically, we previously explicitly used the altair.vegalite.v4.api.Chart module (part of the internal API), but this no longer exists in Altair 5. Instead, it is advised to directly use altair.Chart (see conversation in #6215), which is the official public API that will stay stable.

In addition, this PR also updates our frontend vega dependencies to the latest versions and moves the altair import to lazy loading for performance improvement. This also removes the registered streamlit altair theme, which was only an unofficial workaround for the initial implementation of the Streamlit chart theme.

Since we were anyways already running Vega lite v5 in our frontend for the last few months, I don't expect this update to introduce any incompatibilities. V4 and v5 should be "mostly" compatible anyways. The backend changes are also completely independent of the Altair version since the public API relevant to Streamlit (e.g. altair.Chart) was stable across the versions.

  • What kind of change does this PR introduce?

    • Bugfix
    • Feature
    • Refactoring
    • Other, please describe: Dependency upgrade

🧪 Testing Done

  • Screenshots included
  • Added/Updated unit tests
  • Added/Updated e2e tests

🌐 References

Does this depend on other work, documents, or tickets?


Contribution License Agreement

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

@lukasmasuch lukasmasuch mentioned this pull request May 3, 2023
5 tasks
@lukasmasuch lukasmasuch changed the title Add support for Altair 5 [WIP] Add support for Altair 5 May 3, 2023
@lukasmasuch lukasmasuch added the security-assessment-completed Security assessment has been completed for PR label May 3, 2023
@binste
Copy link
Contributor

binste commented May 9, 2023

@lukasmasuch Just fyi, Altair 5 got released today 🥳

@lukasmasuch lukasmasuch changed the title [WIP] Add support for Altair 5 Add support for Altair 5 May 15, 2023
@lukasmasuch lukasmasuch marked this pull request as ready for review May 15, 2023 20:08
# - Include relevant lower bound for any features we use from our dependencies
# - And include an upper bound that's < NEXT_MAJOR_VERSION
INSTALL_REQUIRES = [
"altair>=3.2.0, <5",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this breaking for Altair 3? We had conversations about 4 to 5, but want to verify the problem with 3.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah Lukas pointed this out, but we already used v4 as well, so we were probably already breaking.

Copy link
Collaborator Author

@lukasmasuch lukasmasuch May 15, 2023

Choose a reason for hiding this comment

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

The 3.x version does not even contain the altair.vegalite.v4.api.Chart import we were using, so I'm pretty sure that we haven't been supporting this version anyways for a while 😅

)
self.assertEqual(spec_dict["data"], {"name": proto.datasets[0].name})
self.assertEqual(spec_dict["mark"], "bar")
self.assertIn(spec_dict["mark"], ["bar", {"type": "bar"}])
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 check both here bar and {"type": "bar"} to allow our tests to run with v4 and v5 versions.

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 good to me - my only concern is how sure we are that we're still compatible with Altair 4 ... and beyond that, how do we ensure that we remain compatible with Altair 4.

  • Maybe we just cross our fingers now, and it gets worked out with @AnOctopus's "test min versions of dependencies in CI" project?
  • Alternately: do we want to require altair >=5, <6 for now, and drop Altair 4 support?

@@ -1,29 +0,0 @@
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we want this test anymore?

Copy link
Collaborator Author

@lukasmasuch lukasmasuch May 19, 2023

Choose a reason for hiding this comment

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

This was just a temporary feature - before we had the theme parameter - that got removed in this PR as a side-effect. It was possible to set the theme via:

alt.themes.enable("streamlit")

But we never advertised it and this has some technical issue related to it, so we introduced the theme parameter with our charting commands instead. So this is just removing some unused tech debt.

def _arrow_altair_chart(
self,
altair_chart: Chart,
altair_chart: "Chart",
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I think you can avoid the quotes for this type annotation if you add a from __future__ import annotations at the top of the file (even though the import is conditional on the TYPE_CHECKING flag)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I didn't know. I will update this later :)


if TYPE_CHECKING:
from altair.vegalite.v4.api import Chart
from altair import Chart
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this "from altair import Chart" import work for Altair 4?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, from altair import Chart is the actual public API that always existed this way :)

@lukasmasuch
Copy link
Collaborator Author

my only concern is how sure we are that we're still compatible with Altair 4

From a Python side, this PR will make Streamli neutral to the underlying vega-lite version since we just use the actual public Altair API: from altair import Chart

However, the frontend part is more relevant regarding the compatibility of the vega-lite specs. We have been running vega-lite spec v5 anyways already in the frontend for a few months (probably accidentally), even though the supported Altair version has been producing v4 spec. Since it didn't cause any issues in the last few months, I don't expect that this change will introduce any additional issues.

@lukasmasuch
Copy link
Collaborator Author

lukasmasuch commented May 19, 2023

@tconkling

Alternately: do we want to require altair >=5, <6 for now, and drop Altair 4 support?

I assume this would probably be a too harsh pin here since Altair 5 just got released

Maybe we just cross our fingers now, and it gets worked out with @AnOctopus's "test min versions of dependencies in CI" project?

I think I will merge this PR after the 1.23 release, so we will have another 4 weeks to maybe get something in for the min version testing. Maybe an MVP with just altair and pandas min versions.

@binste
Copy link
Contributor

binste commented May 19, 2023

From a Python side, this PR will make Streamli neutral to the underlying vega-lite version since we just use the actual public Altair API: from altair import Chart

However, the frontend part is more relevant regarding the compatibility of the vega-lite specs. We have been running vega-lite spec v5 anyways already in the frontend for a few months (probably accidentally), even though the supported Altair version has been producing v4 spec. Since it didn't cause any issues in the last few months, I don't expect that this change will introduce any additional issues.

I fully agree with this, the frontend has already been updated anyway, and I again went through the changes and can't spot anything that would no longer work with Altair 4. Apart from the changes in imports to the altair.Chart API which has always been there, the only change in logic I can find is https://github.com/streamlit/streamlit/pull/6618/files#diff-3b83cc7a5d368917b0a9872a1d9476a9f9bf205a5ad7b388407e451e8d87b5baR604. That statement also works in Altair 4.

I think many users are looking forward to this and so it would be great if it can already be included in the 1.23 release but I also understand if you first want to improve the testing strategy! Thank you in any case, looking forward to it.

@lukasmasuch
Copy link
Collaborator Author

@binste Thanks for all the help and input on the Altair 5 update! 👍

@binste
Copy link
Contributor

binste commented May 24, 2023

Sure, thank you for doing it and always happy to chat about the Altair integration in Streamlit! :)

tconkling added a commit to tconkling/streamlit that referenced this pull request May 30, 2023
* develop: (22 commits)
  enzyme -> react-testing-library (Countdown, Modal, ProgressBar) (streamlit#6744)
  Remove console.log (streamlit#6753)
  Update `st.data_editor` and `st.dataframe` docstrings (streamlit#6752)
  Update `st.data_editor` session state format (streamlit#6711)
  Cypress flaky test fixes (streamlit#6743)
  Document integer size limit for number_input and slider (streamlit#6724)
  Change default theme hash on app init (streamlit#6729)
  Use .genericFonts instead of .fonts to fix bug with theme postMessage. (streamlit#6732)
  Improve startup performance by lazy loading some dependencies (streamlit#6531)
  Add support for Altair 5 (streamlit#6618)
  Update modals (streamlit#6688)
  Improve docstrings for `ttl` and `max_entries` (streamlit#6733)
  Improve `st.columns` docstring (streamlit#6727)
  Removed orphan line in dataframe docstring (streamlit#6734)
  Fix useIsOverflowing dependency array (streamlit#6731)
  Remove experimental from data editor (streamlit#6712)
  Clarify set_page_config docstring and exception message (streamlit#6594)
  Add a config option to disable warning for setting both a widget default and its key in session_state (streamlit#6640)
  Add column configuration API for `st.dataframe` and `st.data_editor` (streamlit#6598)
  Migrate datetime column formatting from date-fns to momentJS (streamlit#6702)
  ...
@vdonato vdonato deleted the feature/altair-5-support branch November 1, 2023 23:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Altair 5

5 participants