Skip to content

Commit

Permalink
fix: dont use mpl backend when running as a script (#1089)
Browse files Browse the repository at this point in the history
* fix: dont use mpl backend when running as script

* add bugs smoke
  • Loading branch information
akshayka authored Apr 8, 2024
1 parent d868519 commit a08c27f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
8 changes: 6 additions & 2 deletions marimo/_output/formatters/matplotlib_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ def package_name() -> str:
def register(self) -> None:
import matplotlib # type: ignore

from marimo._runtime.context import get_global_context
from marimo._runtime.context import (
get_global_context,
runtime_context_installed,
)

get_global_context().set_mpl_installed(True)
from marimo._output import mpl # noqa: F401

matplotlib.use("module://marimo._output.mpl")
if runtime_context_installed():
matplotlib.use("module://marimo._output.mpl")

import base64
import io
Expand Down
9 changes: 9 additions & 0 deletions marimo/_runtime/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,12 @@ def get_context() -> RuntimeContext:
if _THREAD_LOCAL_CONTEXT.runtime_context is None:
raise ContextNotInitializedError
return _THREAD_LOCAL_CONTEXT.runtime_context


def runtime_context_installed() -> bool:
try:
get_context()
except ContextNotInitializedError:
return False
else:
return True
21 changes: 21 additions & 0 deletions marimo/_smoke_tests/bugs/1086.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2024 Marimo. All rights reserved.
import marimo

__generated_with = "0.3.10"
app = marimo.App()


@app.cell
def __():
import matplotlib.pyplot as plt
import numpy as np

plt.plot(np.arange(52))

# better to do plt.gca(), but discovered this translating script
plt.show()
return np, plt


if __name__ == "__main__":
app.run()

0 comments on commit a08c27f

Please sign in to comment.