Skip to content

Provide a way to convert None to null in to_js #3968

@philippjfr

Description

@philippjfr

🚀 Feature

I propose some functionality be added to pyodide.ffi.to_js to allow Python None values to be translated into null values rather than undefined values.

Motivation

Currently pyodide.ffi.to_js converts all encountered None values to undefined on the JS side. In certain scenarios this is problematic because some downstream consumer of the converted value might expect null rather than undefined. One such case are Bokeh event messages. When converting the events generated by Bokeh in Python into JS objects that can be applied to a Bokeh Document they often contain serialized copies of the Model attribute values that have changed and Bokeh's property system makes a very clear distinction between a null value and an undefined value. Bokeh makes this distinction because undefined implies the value has not been set, while null is a perfectly valid value.

Pitch

Ideally I'd simply provide some way to override mappings for specific types, e.g. {None: pyodide.code.run_js('null')}. Alternatively I'd also be happy for a custom dict_converter to be made available that allows overriding the translation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions