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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
import asyncio
import json
from unittest.mock import Mock
import pytest
from marimo._pyodide.streams import (
PyodideStderr,
PyodideStdin,
PyodideStdout,
PyodideStream,
)
from marimo._types.ids import CellId_t
cell_id = CellId_t("test-cell-id")
@pytest.fixture
def pyodide_pipe() -> Mock:
return Mock()
@pytest.fixture
async def pyodide_input_queue() -> asyncio.Queue[str]:
return asyncio.Queue()
@pytest.fixture
def pyodide_(
pyodide_pipe: Mock,
pyodide_input_queue: asyncio.Queue[str],
) -> PyodideStream:
return PyodideStream(pyodide_pipe, pyodide_input_queue, cell_id)
@pytest.fixture
def pyodide_stdout(pyodide_: PyodideStream) -> PyodideStdout:
return PyodideStdout(pyodide_)
@pytest.fixture
def pyodide_stderr(pyodide_: PyodideStream) -> PyodideStderr:
return PyodideStderr(pyodide_)
@pytest.fixture
def pyodide_stdin(pyodide_: PyodideStream) -> PyodideStdin:
# bridge.ts -> put_input sends raw user text; readline() adds the "\n".
stdin = PyodideStdin(pyodide_)
stdin._get_response = lambda: "test input"
return stdin
class TestPyodideStream:
def test_write(self, pyodide_: PyodideStream, pyodide_pipe: Mock) -> None:
data = {"key": "value"}
pyodide_.write(data)
pyodide_pipe.assert_called_once_with(data)
def test_copy_for_thread(
self,
pyodide_: PyodideStream,
pyodide_pipe: Mock,
pyodide_input_queue: asyncio.Queue[str],
) -> None:
copied = pyodide_.copy_for_thread()
assert copied is not pyodide_
assert copied.pipe is pyodide_pipe
assert copied.input_queue is pyodide_input_queue
assert copied.cell_id == pyodide_.cell_id
class TestPyodideStdout:
def test_writable(self, pyodide_stdout: PyodideStdout) -> None:
assert pyodide_stdout.writable() is True
def test_readable(self, pyodide_stdout: PyodideStdout) -> None:
assert pyodide_stdout.readable() is False
def test_seekable(self, pyodide_stdout: PyodideStdout) -> None:
assert pyodide_stdout.seekable() is False
def test_write(
self, pyodide_stdout: PyodideStdout, pyodide_pipe: Mock
) -> None:
data = "test output"
pyodide_stdout.write(data)
assert pyodide_pipe.call_count == 1
msg_bytes = pyodide_pipe.call_args[0][0]
msg = json.loads(msg_bytes)
assert msg["op"] == "cell-op"
assert msg["cell_id"] == pyodide_stdout.stream.cell_id
assert msg["console"]["mimetype"] == "text/plain"
assert msg["console"]["data"] == data
def test_writelines(
self, pyodide_stdout: PyodideStdout, pyodide_pipe: Mock
) -> None:
lines = ["line1\n", "line2\n", "line3\n"]
pyodide_stdout.writelines(lines)
assert pyodide_pipe.call_count == 3
class TestPyodideStderr:
@pytest.mark.parametrize("code", [None, "raise ValueError('failed')"])
def test_traceback_source_forwarding(
self,
pyodide_stderr: PyodideStderr,
pyodide_pipe: Mock,
code: str | None,
) -> None:
pyodide_stderr._write_with_mimetype(
"ValueError: failed",
mimetype="application/vnd.marimo+traceback",
code=code,
)
pyodide_pipe.assert_called_once()
console = json.loads(pyodide_pipe.call_args.args[0])["console"]
assert console["data"] == "ValueError: failed"
assert console["mimetype"] == "application/vnd.marimo+traceback"
if code is None:
assert "code" not in console
else:
assert console["code"] == code
def test_writable(self, pyodide_stderr: PyodideStderr) -> None:
assert pyodide_stderr.writable() is True
def test_readable(self, pyodide_stderr: PyodideStderr) -> None:
assert pyodide_stderr.readable() is False
def test_seekable(self, pyodide_stderr: PyodideStderr) -> None:
assert pyodide_stderr.seekable() is False
def test_write(
self, pyodide_stderr: PyodideStderr, pyodide_pipe: Mock
) -> None:
data = "test error"
pyodide_stderr.write(data)
assert pyodide_pipe.call_count == 1
msg_bytes = pyodide_pipe.call_args[0][0]
msg = json.loads(msg_bytes)
assert msg["op"] == "cell-op"
assert msg["cell_id"] == pyodide_stderr.stream.cell_id
assert msg["console"]["mimetype"] == "text/plain"
assert msg["console"]["data"] == data
def test_writelines(
self, pyodide_stderr: PyodideStderr, pyodide_pipe: Mock
) -> None:
lines = ["error1\n", "error2\n", "error3\n"]
pyodide_stderr.writelines(lines)
assert pyodide_pipe.call_count == 3
class TestPyodideStdin:
def test_writable(self, pyodide_stdin: PyodideStdin) -> None:
assert pyodide_stdin.writable() is False
def test_readable(self, pyodide_stdin: PyodideStdin) -> None:
assert pyodide_stdin.readable() is True
async def test_readline(
self,
pyodide_stdin: PyodideStdin,
pyodide_pipe: Mock,
pyodide_input_queue: asyncio.Queue[str],
) -> None:
await pyodide_input_queue.put("test input")
result = pyodide_stdin.readline()
assert result == "test input\n"
assert pyodide_pipe.call_count == 1
msg_bytes = pyodide_pipe.call_args[0][0]
msg = json.loads(msg_bytes)
assert msg["op"] == "cell-op"
assert msg["cell_id"] == pyodide_stdin.stream.cell_id
assert msg["console"]["mimetype"] == "text/plain"
assert msg["console"]["data"] == ""
async def test_readline_with_prompt(
self,
pyodide_stdin: PyodideStdin,
pyodide_pipe: Mock,
pyodide_input_queue: asyncio.Queue[str],
) -> None:
await pyodide_input_queue.put("test input")
# The _readline_with_prompt primitive returns bare text; the "\n"
# is only added by readline() for file-protocol consumers.
result = pyodide_stdin._readline_with_prompt("Enter: ")
assert result == "test input"
assert pyodide_pipe.call_count == 1
msg_bytes = pyodide_pipe.call_args[0][0]
msg = json.loads(msg_bytes)
assert msg["op"] == "cell-op"
assert msg["cell_id"] == pyodide_stdin.stream.cell_id
assert msg["console"]["mimetype"] == "text/plain"
assert msg["console"]["data"] == "Enter: "
async def test_readlines(
self,
pyodide_stdin: PyodideStdin,
pyodide_pipe: Mock,
pyodide_input_queue: asyncio.Queue[str],
) -> None:
pyodide_stdin._get_response = Mock(return_value="hello")
await pyodide_input_queue.put("hello")
result = pyodide_stdin.readlines()
assert result == ["hello\n"]
assert pyodide_pipe.call_count == 1
msg_bytes = pyodide_pipe.call_args[0][0]
msg = json.loads(msg_bytes)
assert msg["op"] == "cell-op"
assert msg["cell_id"] == pyodide_stdin.stream.cell_id
assert msg["console"]["mimetype"] == "text/plain"
assert msg["console"]["data"] == ""
async def test_readline_empty_submission(
self,
pyodide_stdin: PyodideStdin,
pyodide_input_queue: asyncio.Queue[str],
) -> None:
# Empty submission must be a blank line ("\n"), not "" (= EOF).
pyodide_stdin._get_response = lambda: ""
await pyodide_input_queue.put("")
assert pyodide_stdin.readline() == "\n"