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

Use env variable to configure matplotlib backend #8113

Merged
merged 18 commits into from Feb 10, 2024

Conversation

LukasMasuch
Copy link
Collaborator

@LukasMasuch LukasMasuch commented Feb 8, 2024

Describe your changes

The deprecation of the runner.fixMatplotlib and the decision to always use the Agg backend, made it possible to just configure the matplotlib backend via the config option (also see the previous TODO comment). This prevents an unnecessary import of matplotlib at the server start and allows to lazy load this import.

GitHub Issue Link (if applicable)

Related to #6066

Testing Plan

  • Added unit and e2e tests to make sure that matplotlib is properly lazy-loaded.

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 changed the title Use environment variable for matplotlib fix [WIP] Use environment variable for matplotlib fix Feb 8, 2024
@LukasMasuch LukasMasuch changed the title [WIP] Use environment variable for matplotlib fix Use environment variable to configure matplotlib backend Feb 9, 2024
@LukasMasuch LukasMasuch changed the title Use environment variable to configure matplotlib backend Use env variable to configure matplotlib backend Feb 9, 2024
@LukasMasuch LukasMasuch marked this pull request as ready for review February 9, 2024 17:38
Copy link
Collaborator

@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.

LGTM -- is there any possibility that doing this vs environment var will result in us changing the matplotlib backend for users in places they wouldn't have expected it?

I'd expect this is pretty close to being a total non-issue in practice, but just double-checking.

Comment on lines 48 to 56
# Set Matplotlib backend to avoid a crash.
# The default Matplotlib backend crashes Python on OSX when run on a thread
# that's not the main thread, so here we set a safer backend as a fix.
# This fix is OS-independent. We didn't see a good reason to make this
# Mac-only. Consistency within Streamlit seemed more important.
# This needs to run before any other import of matplotlib could happen.
import os as _os

_os.environ["MPLBACKEND"] = "Agg"
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'd probably be good to mention in the comment that this needs to stay where it is before any other imports to ensure that it occurs before matplotlib has a chance to be imported

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See the last line of comment above the os :)

This needs to run before any other import of matplotlib could happen.

I will copy this moment down above the environ and add an Important to make this more prominent.

@LukasMasuch
Copy link
Collaborator Author

LukasMasuch commented Feb 10, 2024

@vdonato

is there any possibility that doing this vs environment var will result in us changing the matplotlib backend for users in places they wouldn't have expected it?

There is definitely a risk. I think I'm ~80% confident it shouldn't have any negative side effects. I will be extra careful to monitor the forum and Github issues for anything Matplotlib-related. And will get this reverted via a patch if anything concerning comes up.

@LukasMasuch LukasMasuch merged commit df3e6b3 into develop Feb 10, 2024
38 checks passed
@LukasMasuch LukasMasuch deleted the refactor/use-env-var-for-matplotlib-fix branch February 10, 2024 15:55
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Apr 16, 2024
## Describe your changes

The deprecation of the `runner.fixMatplotlib` and the decision to always
use the `Agg` backend, made it possible to just configure the matplotlib
backend via the config option (also see the previous TODO comment). This
prevents an unnecessary import of matplotlib at the server start and
allows to lazy load this import.

## GitHub Issue Link (if applicable)

Related to streamlit#6066

## Testing Plan

- Added unit and e2e tests to make sure that `matplotlib` is properly
lazy-loaded.

---

**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

2 participants