Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Fix: expose port via os to allow mpl to work in docker (#2853)

Max Caldwell2962a339af78
diff --git a/docs/guides/deploying/deploying_docker.md b/docs/guides/deploying/deploying_docker.md
index 32af45e568..f023b5ce6d 100644
--- a/docs/guides/deploying/deploying_docker.md
+++ b/docs/guides/deploying/deploying_docker.md
@@ -75,6 +75,19 @@ docker run -p 8080:8080 -it my_app
After verifying that your application runs without errors, you can use these files to deploy your application on your preferred cloud provider that supports dockerized applications.
+### Using Interactive Matplotlib Plots in Docker
+
+If your notebook uses `mo.mpl.interactive()` for interactive matplotlib plots, you'll need to make some additional configurations in your Dockerfile:
+
+```Dockerfile
+# Add these lines before the CMD instruction
+ENV MARIMO_MPL_HOST="0.0.0.0" # same as main host from `marimo run`.
+ENV MARIMO_MPL_SECURE=true # if you are running on https
+EXPOSE 10000 # Expose in addition to your main application port
+```
+
+This exposes the additional CSS and JavaScript required.
+
## Health checks
You can add a health check to your Dockerfile to ensure that your application is running as expected. This is especially useful when deploying to a cloud provider.
diff --git a/docs/guides/working_with_data/plotting.md b/docs/guides/working_with_data/plotting.md
index 345f30a305..a9bb3363e4 100644
--- a/docs/guides/working_with_data/plotting.md
+++ b/docs/guides/working_with_data/plotting.md
@@ -283,5 +283,6 @@ If you want to output the plot in the console area, use `plt.show()` or
### Interactive plots
To make matplotlib plots interactive, use
-[mo.mpl.interactive](../api/plotting.md#marimo.mpl.interactive).
+[mo.mpl.interactive](/api/plotting.md#marimo.mpl.interactive).
(Matplotlib plots are not yet reactive.)
+For additional instructions when deploying an interactive matplotlib plot in Docker, see [Interactive Matplotlib Plots in Docker](/guides/deploying/deploying_docker.md#using-interactive-matplotlib-plots-in-docker)
diff --git a/marimo/_plugins/stateless/mpl/_mpl.py b/marimo/_plugins/stateless/mpl/_mpl.py
index f6410e7f90..20b95db58a 100644
--- a/marimo/_plugins/stateless/mpl/_mpl.py
+++ b/marimo/_plugins/stateless/mpl/_mpl.py
@@ -11,6 +11,7 @@ import asyncio
import html
import io
import mimetypes
+import os
import signal
import threading
import time
@@ -58,15 +59,52 @@ class FigureManagers:
figure_managers = FigureManagers()
-def _template(
- host: str,
- port: int,
- fig_id: str,
-) -> str:
+def _get_host() -> str:
+ """
+ Get the host from environment variable or fall back to localhost.
+ """
+ host = os.environ.get("MARIMO_MPL_HOST", "localhost")
+ if not host or not isinstance(host, str):
+ return "localhost"
+ if "://" in host:
+ raise ValueError(
+ f"Invalid host '{host}': should not include protocol (http:// or https://)"
+ )
+ if "/" in host:
+ raise ValueError(f"Invalid host '{host}': should not include paths")
+ if ":" in host:
+ raise ValueError(
+ f"Invalid host '{host}': should not include port numbers"
+ )
+ return host
+
+
+def _get_secure() -> bool:
+ """
+ Get the secure status from environment variable or fall back to False.
+ """
+ secure = os.environ.get("MARIMO_MPL_SECURE", "false")
+ if not secure or not isinstance(secure, str):
+ return False
+ secure = secure.lower().strip()
+ if secure in ("true", "1", "yes", "on"):
+ return True
+ if secure in ("false", "0", "no", "off"):
+ return False
+
+ raise ValueError(
+ f"Invalid secure value '{secure}': should be 'true' or 'false'"
+ )
+
+
+def _template(host: str, port: int, fig_id: str, secure: bool = False) -> str:
+ ws_protocol = "wss" if secure else "ws"
+ http_protocol = "https" if secure else "http"
+
return html_content % {
- "ws_uri": f"ws://{host}:{port}/ws?figure={fig_id}",
+ "ws_uri": f"{ws_protocol}://{host}:{port}/ws?figure={fig_id}",
"fig_id": fig_id,
- "base_url": f"http://{host}:{port}",
+ "base_url": f"{http_protocol}://{host}:{port}",
}
@@ -195,17 +233,23 @@ def create_application(
_app: Optional[Starlette] = None
-def get_or_create_application() -> Starlette:
+def get_or_create_application(
+ app_host: Optional[str] = None,
+ free_port: Optional[int] = None,
+ secure_host: Optional[bool] = None,
+) -> Starlette:
global _app
import uvicorn
if _app is None:
- host = "localhost"
- port = find_free_port(10_000)
+ host = app_host if app_host is not None else _get_host()
+ port = free_port if free_port is not None else find_free_port(10_000)
+ secure = secure_host if secure_host is not None else _get_secure()
app = create_application(host, port)
app.state.host = host
app.state.port = port
+ app.state.secure = secure
_app = app
def start_server() -> None:
@@ -294,6 +338,7 @@ def interactive(figure: Union[Figure, Axes]) -> Html:
application = get_or_create_application()
host = application.state.host
port = application.state.port
+ secure = application.state.secure
class CleanupHandle(CellLifecycleItem):
def create(self, context: RuntimeContext) -> None:
@@ -310,7 +355,7 @@ def interactive(figure: Union[Figure, Axes]) -> Html:
ctx.cell_lifecycle_registry.add(CleanupHandle())
ctx.stream.cell_id = ctx.execution_context.cell_id
- content = _template(host, port, str(figure_manager.num))
+ content = _template(host, port, str(figure_manager.num), secure)
return Html(
h.iframe(