Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: increase data serialization speed #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/nested_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"getDetailRowData": JsCode(
"""function (params) {
console.log(params);
params.successCallback(JSON.parse(params.data.callRecords));
params.successCallback(params.data.callRecords);
}"""
).js_code,
},
Expand Down
27 changes: 1 addition & 26 deletions st_aggrid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import os
import streamlit.components.v1 as components
import pandas as pd
import numpy as np
import simplejson
import warnings
from dotenv import load_dotenv
import typing

from st_aggrid.grid_options_builder import GridOptionsBuilder
from st_aggrid.shared import GridUpdateMode, DataReturnMode, JsCode, walk_gridOptions
from numbers import Number
load_dotenv()

_RELEASE = os.getenv("AGGRID_RELEASE",'true').lower() == 'true'
Expand Down Expand Up @@ -155,30 +153,7 @@ def AgGrid(
gb = GridOptionsBuilder.from_dataframe(dataframe,**default_column_parameters)
gridOptions = gb.build()

def get_row_data(df):
def cast_to_serializable(value):
if isinstance(value, pd.DataFrame):
return get_row_data(value)

isoformat = getattr(value, 'isoformat', None)

if ((isoformat) and callable(isoformat)):
return isoformat()

elif isinstance(value, Number):
if (np.isnan(value) or np.isinf(value)):
return value.__str__()

return value
else:
return value.__str__()

json_frame = df.applymap(cast_to_serializable)
row_data = json_frame.to_dict(orient="records")
row_data = simplejson.dumps(row_data, ignore_nan=True)
return row_data

row_data = get_row_data(dataframe)
row_data = dataframe.to_json(orient="records", date_format="iso")

if allow_unsafe_jscode:
walk_gridOptions(gridOptions, lambda v: v.js_code if isinstance(v, JsCode) else v)
Expand Down