adamerose / PandasGUI

A GUI for Pandas DataFrames
MIT No Attribution
3.18k stars 230 forks source link

Real-time update #107

Closed hrostami closed 3 years ago

hrostami commented 3 years ago

Hey, I'm working on an algorithmic trading bot and for now, it updates every minute. I need to just see the dataframe for current trades and be able to edit it if needed and then export it to a .csv and when the bot updates data again after a minute, get updated dataframe in pandasgui. when I try to use refresh it just says No matching DataFrames found to refresh

adamerose commented 3 years ago

If you modify the underlying DataFrame and want the UI to rerender you need to call the update method of the PandasGuiDataFrameStore. refresh is a PandasGui method that will replace all GUI DataFrames with the current DataFrame of the same name from the scope show was called, only useful during iPython EDA.

I could add a helper method to PandasGui to update the UI for all DataFrames. Maybe I'll rename refresh to something else since I've had similar questions before

hrostami commented 3 years ago

thanks a lot, yeah that would be great. Could you please give an example of how to use update? I looked at PandasGuiDataFrameStore but couldn't figure out how to use it

adamerose commented 3 years ago

So after looking at this more I decided not to add an update helper method because I think your use case can't be made that easy. It sounds like you're calling show on a DataFrame and then modifying that DataFrame and wanting those changes reflected in the UI. This can't be done automatically because PandasGui works with copies of the passed DataFrames, it doesn't ever modify or re-read the original.

So you have two solutions

  1. Directly modify the DataFrames in the GUI.
  2. Modify your originals and replace the one in the GUI with that every time you make a change. The downside of this is that replacing a DataFrame right now will wipe away any GUI state like sorting or filters, this might be improved in the future.

Example of option 1:

import pandas as pd
from pandasgui import show

df = pd.DataFrame({'a': [1, 2, 3]})
gui = show(df)

# This is the PandasGuiDataFrameStore instance that wraps internal DataFrames
pgdf = gui.store.data['df']

# Change the underlying DataFrame then update the UI.
pgdf.df['a'] = pgdf.df['a'] * 100
pgdf.update()

Example of option 2:

import pandas as pd
from pandasgui import show

df = pd.DataFrame({'a': [1, 2, 3]})
gui = show(df)

df['a'] = df['a'] * 100
gui.store.remove_dataframe('df')
gui.store.add_dataframe(df, 'df')
wskxjtzq commented 3 years ago

I ran "Example of option 2" using Pycharm. But it can only run to "gui = show(df)".

The window is displayed successfully.

But the program is blocked and it cannot continue to run subsequent codes. such as:

df['a'] = df['a'] * 100 gui.store.remove_dataframe('df') gui.store.add_dataframe(df,'df')

Due to blocking, these three lines of code did not run. Therefore, the window cannot be updated.

How can I deal with this blocking problem?

adamerose commented 3 years ago

@wskxjtzq If you're running as a script then it will be blocking and you wouldn't be able to execute code. Running PandasGUI should be non-blocking automatically in IPython, but if that's failing you can do gui = show(df, settings={'block': False})