Skip to content

Commit

Permalink
fix for incompatible str types when directly altering state of data i…
Browse files Browse the repository at this point in the history
…n running D-Tale instance
  • Loading branch information
Andrew Schonfeld authored and aschonfeld committed Oct 29, 2019
1 parent 1f319ae commit 056020c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 14 additions & 3 deletions dtale/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ def build_dtypes_state(data):
return [dict(name=c, dtype=dtypes[c], index=i) for i, c in enumerate(data.columns)]


def format_data(data):
if isinstance(data, (pd.DatetimeIndex, pd.MultiIndex)):
data = data.to_frame(index=False)

logger.debug('pytest: {}, flask: {}'.format(running_with_pytest(), running_with_flask()))
index = [str(i) for i in make_list(data.index.name or data.index.names) if i is not None]
data = data.reset_index().drop('index', axis=1, errors='ignore')
data.columns = [str(c) for c in data.columns]
return data, index


def startup(data=None, data_loader=None, port=None, name=None):
"""
Loads and stores data globally
Expand All @@ -109,9 +120,7 @@ def startup(data=None, data_loader=None, port=None, name=None):
data = data.to_frame(index=False)

logger.debug('pytest: {}, flask: {}'.format(running_with_pytest(), running_with_flask()))
curr_index = [str(i) for i in make_list(data.index.name or data.index.names) if i is not None]
data = data.reset_index().drop('index', axis=1, errors='ignore')
data.columns = [str(c) for c in data.columns]
data, curr_index = format_data(data)
port_key = str(port)
if port_key in SETTINGS:
curr_settings = SETTINGS[port_key]
Expand Down Expand Up @@ -356,6 +365,8 @@ def get_data():
# state of the dataframe (EX: d.data['new_col'] = 'foo')
curr_dtypes = [c['name'] for c in DTYPES[port]]
if any(c not in curr_dtypes for c in data.columns):
data, _ = format_data(data)
DATA[port] = data
DTYPES[port] = build_dtypes_state(data)

params = retrieve_grid_params(request)
Expand Down
1 change: 1 addition & 0 deletions tests/dtale/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def test_get_data(unittest, test_data):

with app.test_client() as c:
with ExitStack() as stack:
test_data, _ = views.format_data(test_data)
stack.enter_context(mock.patch('dtale.views.DATA', {c.port: test_data}))
stack.enter_context(mock.patch('dtale.views.DTYPES', {c.port: views.build_dtypes_state(test_data)}))
response = c.get('/dtale/data')
Expand Down

0 comments on commit 056020c

Please sign in to comment.