Open MarcSkovMadsen opened 2 weeks ago
To support a custom DataFrame
like parameter you need to update panel.io.datamodel.PARAM_MAPPING
:
import panel as pn
import param
from panel.custom import JSComponent
import polars as pl
import pandas as pd
from panel_gwalker._tabular_data import TabularData
from panel.io.datamodel import PARAM_MAPPING, bp
pn.extension()
_VALID_CLASSES = (
"<class 'pandas.core.frame.DataFrame'>",
"<class 'polars.dataframe.frame.DataFrame'>",
)
class TabularData(param.Parameter):
def _validate(self, val):
super()._validate(val=val)
try:
if str(val.__class__) in _VALID_CLASSES:
return
except:
pass
msg=f"A value of type '{type(val)}' is not valid"
raise ValueError(msg)
def _column_datasource_from_polars_df(df):
df = df.to_pandas()
return ColumnDataSource._data_from_df(df)
PARAM_MAPPING.update({
TabularData: lambda p, kwargs: (
bp.ColumnData(bp.Any, bp.Seq(bp.Any), **kwargs),
[(bp.PandasDataFrame, _column_datasource_from_polars_df)],
),
})
class GraphicWalker(JSComponent):
object = param.DataFrame()
_esm = '''
export function render({ model, el }) {
console.log(model.object)
el.innerHTML = JSON.stringify(model.object)
}
'''
GraphicWalker(object=pd.DataFrame({"x": [1,2,3]})).servable()
I can see that bp.PandasDataFrame
is defined as below
class PandasDataFrame(Property["DataFrame"]):
""" Accept Pandas DataFrame values.
This property only exists to support type validation, e.g. for "accepts"
clauses. It is not serializable itself, and is not useful to add to
Bokeh models directly.
"""
def validate(self, value: Any, detail: bool = True) -> None:
super().validate(value, detail)
import pandas as pd
if isinstance(value, pd.DataFrame):
return
msg = "" if not detail else f"expected Pandas DataFrame, got {value!r}"
raise ValueError(msg)
Someday Bokeh should probably support a more general DataFrame
property like narwhals.typing.IntoFrameT
. See https://narwhals-dev.github.io/narwhals/basics/dataframe/.
Any plans about more general support for dataframes @mattpap?
There's an ongoing discussion regarding this subject in https://github.com/bokeh/bokeh/issues/13780.
I will apply a temporary fix that will convert polars DataFrames to pandas for now.
I will apply a temporary fix that will convert polars DataFrames to pandas for now.
Would it be an idea to finalize the proof of concept in https://github.com/panel-extensions/panel-graphic-walker/blob/89d6bac66d119d6ad1f75dfe27ec984d18d895ed/src/panel_gwalker/_tabular_data.py#L1 first. To me it looks very close to the right solution.
Then apply in panel?
What is missing is usage of narwhals to enable any tabular/ dataframe like data source. Not just polars.
Started in Param: https://github.com/holoviz/param/pull/975
panel==1.5.3
I would like to add support for Polars DataFrame to
GraphicWalker.object
. Its unclear to me whether this is supported and how. I guess special care is taken about pandas dataframe serialization? Please support it.