| url | https://sqlrooms.org/api/python.md |
|---|
Experimental: This package's API and behavior may change between releases.
Python runtime and block primitives for SQLRooms block documents.
This package provides:
- persisted Python block state schemas for code, inputs, outputs, requirements, execution policy, and bounded last-result summaries
createPythonSlice()for durable block state and runtime result updates- a Pyodide worker runtime adapter with SQLRooms host-query bridge support
PythonBlockandcreatePythonBlockDefinition()for embeddable block document blockscreatePythonBlockCommands()for command-backed create, update, run, and clear operations
If no runtime adapter is configured, running a block records a bounded error result instead of executing hidden Python.
import {
createPythonSlice,
PythonSliceConfig,
type PythonSliceState,
} from '@sqlrooms/python/block';
type RoomState = PythonSliceState;
const sliceConfigSchemas = {
python: PythonSliceConfig,
};
const storeSlices = {
...createPythonSlice()(set, get, store),
};Adapters implement a small execution interface:
const adapter = {
id: 'pyodide',
status: async () => ({state: 'ready'}),
execute: async (request, host) => ({
executionId: request.executionId,
status: 'success',
stdout: '',
stderr: '',
outputs: [{type: 'json', name: 'result', value: 42}],
durationMs: 5,
}),
};Pass the adapter to createPythonSlice({runtimeAdapter: adapter}).
The bundled Pyodide adapter captures the global result value, or the final
expression value when no result global is assigned. It returns JSON, text,
HTML, and Vega-Lite outputs when Python objects expose common rich-display
methods such as _repr_mimebundle_(), _repr_html_(), or Altair to_dict().
HTML and Vega-Lite previews are rendered in sandboxed iframes by
PythonBlock.
When user code imports altair, the Pyodide worker installs Altair on demand
through micropip. The SQLRooms bridge exposes sqlrooms.query() and
sqlrooms.query_df() as pandas DataFrames, so query results can be passed
directly to libraries such as Altair. Use sqlrooms.query_records() for plain
row records and sqlrooms.query_raw() when column metadata or rowCount is
needed.
@sqlrooms/python/runtimeexports runtime contracts and the Pyodide adapter.@sqlrooms/python/blockexports the React block, Zustand slice, commands, and block-facing state schemas.@sqlrooms/pythonis the curated root entrypoint.
Use createPythonBlockCommands() alongside the block-document commands to
expose:
block-document.add-python-blockblock-document.update-python-block-codeblock-document.run-python-blockblock-document.clear-python-block-result
Python block commands target block-document artifacts and use canonical
block-document.* IDs and “block document” descriptions. Hosts can customize
commandGroup for UI grouping; artifact type, label, and command namespace
overrides are not supported.
- CreatePyodidePythonRuntimeAdapterOptions
- PythonBlockRenderProps
- PythonBlockProps
- CreatePythonBlockDefinitionOptions
- PythonBlockCommandSuffix
- CreatePythonBlockCommandsOptions
- EnsurePythonBlockOptions
- PythonBlockRuntimeState
- PythonSliceState
- CreatePythonSliceOptions
- PythonRuntimeAdapterKind
- PythonExecutionStatus
- PythonInput
- PythonOutputDeclaration
- PythonRequirementSpec
- PythonExecutionPolicy
- PythonRuntimeSpec
- PythonExecutionOutput
- PythonExecutionError
- PythonResultSummary
- PythonBlockState
- PythonSliceConfig
- PythonExecutionRequest
- PythonExecutionResult
- PythonRuntimeCapability
- PythonTabularInput
- PythonTabularOutput
- ReadonlySqlRequest
- PythonSchemaRequest
- PythonSchemaSummary
- PythonTableInputSpec
- PythonTableOutputSpec
- PythonAssetOutput
- PythonResolvedPackageSpec
- PythonRuntimeHost
- PythonRuntimeAdapter
- PythonBlock
- PYTHON_BLOCK_TYPE
- PYTHON_BLOCK_COMMAND_SUFFIXES
- PythonRuntimeAdapterKind
- PythonExecutionStatus
- PythonInput
- PythonOutputDeclaration
- PythonRequirementSpec
- PythonExecutionPolicy
- PythonRuntimeSpec
- PythonExecutionOutput
- PythonExecutionError
- PythonResultSummary
- PythonBlockState
- PythonSliceConfig
- createPyodidePythonRuntimeAdapter
- createPythonBlockDefinition
- createPythonBlockCommands
- createPythonSlice
- useStoreWithPython
Renames and re-exports PythonExecutionPolicy
Renames and re-exports PythonInput
Renames and re-exports PythonOutputDeclaration
Renames and re-exports PythonResultSummary
Renames and re-exports PythonRuntimeSpec
Renames and re-exports PythonSliceConfig
Renames and re-exports PythonBlockState
Renames and re-exports PythonExecutionError
Renames and re-exports PythonExecutionOutput
Renames and re-exports PythonExecutionStatus
Renames and re-exports PythonRequirementSpec
Renames and re-exports PythonRuntimeAdapterKind