Uses mleibman/SlickGrid. Examples for slickgrid: Examples · mleibman/SlickGrid Wiki
Alt fork for slickgrid: 6pac/SlickGrid (useful since slickgrid is not being updated right now.)
pip install git+https://github.com/quantopian/qgrid
In [1]:
import warnings
warnings.filterwarnings('ignore')
In [2]:
NEED_TO_INSTALL = True
import qgrid
if NEED_TO_INSTALL:
qgrid.nbinstall(overwrite=True) # copies javascript dependencies to your /nbextensions folder
qgrid.set_defaults(remote_js=False, precision=4)
In [3]:
# https://github.com/quantopian/qgrid/pull/49#issue-121942807
import qgrid
from ipywidgets import widgets
from ipywidgets.widgets import Button, VBox
from IPython.display import display
import pandas as pd
from pandas import DataFrame
pd.set_option('display.max_rows', 8)
from pandas.io.data import get_data_yahoo
spy = get_data_yahoo(
symbols='SPY',
start=pd.Timestamp('2011-01-01'),
end=pd.Timestamp('2014-01-01'),
adjust_price=True,
)
# use an integer index so we can add rows
spy = spy.reset_index()
qgrid.nbinstall(overwrite=True)
grid = qgrid.QGridWidget(df=spy)
button = Button(description='Update')
status = widgets.Text(
background='red'
)
def update(button):
new = get_data_yahoo(
symbols='SPY',
start=pd.Timestamp('2011-01-01'),
end=pd.Timestamp('2014-01-01'),
adjust_price=True,
)
grid.df = new
button.on_click(update)
def selection_change(widget, changes, *args, **kwargs):
print (widget, changes, args, kwargs)
#print (changes)
if changes.get('type') == 'selection_change':
print ('selection_change', changes.get('rows'), widget.get_selected_rows())
status.value = unicode(widget.get_selected_rows())
elif changes.get('type') == 'cell_change':
print ('cell_change', changes)
grid.on_msg(selection_change)
display(VBox((button, status, grid)))
some events coming from qgrid:
cell_change
(qgrid/qgrid.widget.js at 7a3a5e6d1bcd0b2caf092bf2516b654700adf7cc · quantopian/qgrid)-- returns row
, column
, value
based on "row", "cell", "item") -- row is row number, cell is the column number, item is an object indexed by column namesselection_change
add_row
remove_row
What to do next:
QGridWidget
.What can we do with qgrid?
In [ ]:
columns = ['id', 'name','color','marbles']
data = [
{'id':0, 'name': 'Fred', 'color':'red', 'marbles':2},
{'id':1, 'name': 'Zhang', 'color':'blue', 'marbles':5},
{'id':2, 'name': 'Deb', 'color':'orange', 'marbles':0}
]
df = DataFrame(data, columns=columns)
df
In [ ]:
df.loc[10] = {'id':2, 'name': 'Kim', 'color':'orange', 'marbles':0}
df
In [ ]:
df.index
In [ ]:
data_widget = qgrid.QGridWidget(df=df, remote_js=False)
add_row = widgets.Button(description="Add Row")
add_row.on_click(data_widget.add_row)
rem_row = widgets.Button(description="Remove Row")
rem_row.on_click(data_widget.remove_row)
display(widgets.HBox((add_row, rem_row)), data_widget)
In [ ]:
df.columns
In [ ]:
df
In [ ]:
len(df)
In [ ]:
# replace df if we need ...
data_widget.df = df
data_widget._df_changed()
In [ ]:
data_widget
In [ ]:
w = widgets.Text()
def text_event(widget, changes, *args, **kwargs):
print (widget, changes, args, kwargs)
print (changes)
w.on_msg(text_event)
display(w)
In [ ]:
# thought this would be interesting but I think the only event supported is submit.
class MyText(widgets.Text):
def _handle_string_msg(self, _, content, buffers):
"""Handle a msg from the front-end.
Parameters
----------
content: dict
Content of the msg."""
super(MyText, self)._handle_string_msg( _, content, buffers)
print (_, content, buffers)
w = MyText()
w
In [ ]:
assert False
In [ ]:
%%javascript
$('div.widget-area button.close:visible').click();
In [ ]: