Skip to main content

📊 Output

The Output component displays the results of code execution, supporting rich output types including text, HTML, images, plots, and IPyWidgets.

Overview

The Output component is a versatile display area that renders execution results from Jupyter kernels. It can work standalone or be combined with an editor to create interactive code execution experiences.

Installation

npm install @datalayer/jupyter-react
# or
yarn add @datalayer/jupyter-react

Basic Usage

import { Output, JupyterReactTheme, useJupyter } from '@datalayer/jupyter-react';

function App() {
const { defaultKernel } = useJupyter({
startDefaultKernel: true,
});

return (
<JupyterReactTheme>
{defaultKernel && (
<Output
kernel={defaultKernel}
code="print('Hello, World!')"
autoRun={true}
showEditor={true}
/>
)}
</JupyterReactTheme>
);
}

Props

PropTypeDefaultDescription
codestring''The code to execute
autoRunbooleanfalseAutomatically run code on mount
showEditorbooleanfalseShow code editor above output
showControlbooleantrueShow run/stop control buttons
showKernelProgressBarbooleantrueShow kernel execution progress
outputsIOutput[]Default messageInitial outputs to display
kernelKernelDefault kernelSpecific kernel to use
luminobooleantrueUse Lumino widget rendering
disableRunbooleanfalseDisable code execution
executeTriggernumber0Trigger execution when changed
clearTriggernumber0Trigger output clear when changed
toolbarPosition'up' | 'middle' | 'none''up'Position of the toolbar
suppressCodeExecutionErrorsbooleanfalseSuppress error outputs
codePrestring-Code to run before main code
insertTextfunction-Function to insert text into editor
onExecutionPhaseChangedfunction-Callback for execution phase changes
receiptstring-String to match for grade success

Examples

Display Pre-computed Outputs

import { Output, JupyterReactTheme } from '@datalayer/jupyter-react';

function PrecomputedOutput() {
const outputs = [
{
output_type: 'execute_result',
data: {
'text/html': ['<h2>Pre-computed Result</h2>'],
},
execution_count: 1,
metadata: {},
},
];

return (
<JupyterReactTheme>
<Output outputs={outputs} showEditor={false} />
</JupyterReactTheme>
);
}

Interactive Code Editor with Output

import { Output, JupyterReactTheme, useJupyter } from '@datalayer/jupyter-react';

function InteractiveOutput() {
const { defaultKernel } = useJupyter({
startDefaultKernel: true,
});

return (
<JupyterReactTheme>
{defaultKernel && (
<Output
kernel={defaultKernel}
code={`import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.title('Sine Wave')
plt.show()`}
showEditor={true}
showControl={true}
autoRun={false}
/>
)}
</JupyterReactTheme>
);
}

Programmatic Execution

import { useState } from 'react';
import { Output, JupyterReactTheme, useJupyter } from '@datalayer/jupyter-react';

function ProgrammaticExecution() {
const [trigger, setTrigger] = useState(0);
const { defaultKernel } = useJupyter({
startDefaultKernel: true,
});

return (
<JupyterReactTheme>
{defaultKernel && (
<>
<button onClick={() => setTrigger(t => t + 1)}>Run Code</button>
<Output
kernel={defaultKernel}
code="print('Executed!')"
executeTrigger={trigger}
showEditor={false}
/>
</>
)}
</JupyterReactTheme>
);
}

Execution Phase Tracking

import { Output, JupyterReactTheme, useJupyter } from '@datalayer/jupyter-react';

function TrackedOutput() {
const { defaultKernel } = useJupyter({
startDefaultKernel: true,
});

const handlePhaseChange = phaseOutput => {
console.log('Phase:', phaseOutput.phase);
console.log('Output:', phaseOutput.output);
};

return (
<JupyterReactTheme>
{defaultKernel && (
<Output
kernel={defaultKernel}
code="for i in range(5): print(i)"
autoRun={true}
onExecutionPhaseChanged={handlePhaseChange}
/>
)}
</JupyterReactTheme>
);
}

Output Types Supported

The Output component can render various Jupyter output types:

  • Text/Plain: Plain text output
  • Text/HTML: Rendered HTML content
  • Image/PNG: PNG images (matplotlib plots, etc.)
  • Image/JPEG: JPEG images
  • Image/SVG+XML: SVG graphics
  • Application/JSON: JSON data
  • Application/vnd.jupyter.widget-view+json: IPyWidgets
  • Text/LaTeX: Mathematical equations
  • Text/Markdown: Markdown content

State Management

The Output component integrates with Zustand for state management:

import { useOutputsStore } from '@datalayer/jupyter-react';

function OutputController() {
const outputStore = useOutputsStore();

// Get adapter for an output
const adapter = outputStore.getAdapter('my-output-id');

// Execute code programmatically
adapter?.execute('print("Hello")');

// Get current outputs
const model = outputStore.getModel('my-output-id');

return null;
}

Styling

The Output component uses JupyterLab's styling by default. You can customize it:

.dla-Output-container {
border: 1px solid #e0e0e0;
border-radius: 4px;
padding: 8px;
}

.dla-Output-editor {
margin-bottom: 8px;
}

.jp-OutputArea-output {
/* Customize output area */
}
  • Cell - Full cell with input and output
  • Notebook - Collection of cells
  • Console - REPL-style interface