如何在 streamlit web 应用程序中显示 pydatatable 帧输出?
How to display pydatatable frame output in streamlit web app?
我有一个脚本可以在 streamlit 应用程序中显示数据表帧输出:
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)
执行此脚本时,它会发出一个错误:
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')
如果我将此数据表框架转换为 pandas 框架,它可以完美显示输出。
似乎streamlit.table()
不支持数据表框架:https://docs.streamlit.io/en/stable/api.html#streamlit.table
所以目前,唯一的选择是将您的框架转换为任何受支持的框架,即 pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, Iterable, dict, or None
。幸运的是,数据表支持转换为所有这些格式:https://datatable.readthedocs.io/en/latest/api/frame.html#convert-into-other-formats
我有一个脚本可以在 streamlit 应用程序中显示数据表帧输出:
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)
执行此脚本时,它会发出一个错误:
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')
如果我将此数据表框架转换为 pandas 框架,它可以完美显示输出。
似乎streamlit.table()
不支持数据表框架:https://docs.streamlit.io/en/stable/api.html#streamlit.table
所以目前,唯一的选择是将您的框架转换为任何受支持的框架,即 pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, Iterable, dict, or None
。幸运的是,数据表支持转换为所有这些格式:https://datatable.readthedocs.io/en/latest/api/frame.html#convert-into-other-formats