pythonstreamlitpy-datatable

How to display pydatatable frame output in streamlit web app?


I have a script to display datatable frame output in streamlit app as:

import datatable as dt
import streamlit as st
import pandas as pd

st.set_page_config(
page_title="pydatatable demo",
layout="wide",
initial_sidebar_state="expanded")

DT = dt.Frame({
    'class':['a','b','c','d','e'],
    'score':[1,2,3,4,5]
})

st.table(DT)

On executing this script it sends out an error as:

2021-08-28 05:34:40.905 Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/streamlit/script_runner.py", line 350, in _run_script
    exec(code, module.__dict__)
  File "/content/pydt_demo.py", line 16, in <module>
    st.table(DT)
  File "/usr/local/lib/python3.7/dist-packages/streamlit/elements/dataframe_selector.py", line 118, in table
    return self.dg._arrow_table(data)
  File "/usr/local/lib/python3.7/dist-packages/streamlit/elements/arrow.py", line 119, in _arrow_table
    marshall(proto, data, default_uuid)
  File "/usr/local/lib/python3.7/dist-packages/streamlit/elements/arrow.py", line 160, in marshall
    proto.data = type_util.data_frame_to_bytes(df)
  File "/usr/local/lib/python3.7/dist-packages/streamlit/type_util.py", line 371, in data_frame_to_bytes
    table = pa.Table.from_pandas(df)
  File "pyarrow/table.pxi", line 1479, in pyarrow.lib.Table.from_pandas
  File "/usr/local/lib/python3.7/dist-packages/pyarrow/pandas_compat.py", line 591, in dataframe_to_arrays
    for c, f in zip(columns_to_convert, convert_fields)]
  File "/usr/local/lib/python3.7/dist-packages/pyarrow/pandas_compat.py", line 591, in <listcomp>
    for c, f in zip(columns_to_convert, convert_fields)]
  File "/usr/local/lib/python3.7/dist-packages/pyarrow/pandas_compat.py", line 577, in convert_column
    raise e
  File "/usr/local/lib/python3.7/dist-packages/pyarrow/pandas_compat.py", line 571, in convert_column
    result = pa.array(col, type=type_, from_pandas=True, safe=safe)
  File "pyarrow/array.pxi", line 301, in pyarrow.lib.array
  File "pyarrow/array.pxi", line 83, in pyarrow.lib._ndarray_to_array
  File "pyarrow/error.pxi", line 84, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: ('Could not convert    | class\n   | str32\n-- + -----\n 0 | a    \n 1 | b    \n 2 | c    \n 3 | d    \n 4 | e    \n[5 rows x 1 column]\n with type datatable.Frame: did not recognize Python value type when inferring an Arrow data type', 'Conversion failed for column 0 with type object')

Here as a workaround if i convert this datatable Frame to pandas frame it shows output perfectly.


Solution

  • It doesn't seem like streamlit.table() supports datatable frames: https://docs.streamlit.io/en/stable/api.html#streamlit.table

    So for the moment, the only option is to convert your frame into anything that is supported, i.e. pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, Iterable, dict, or None. Luckily, datatable supports conversion into all of these formats: https://datatable.readthedocs.io/en/latest/api/frame.html#convert-into-other-formats