Basically what I'm trying to do is make a pandas dataframe clickable and saving the clicks the user made.
Thought about using widgets for that.
so far I have this:
import pandas as pd
from IPython.display import display, HTML
from ipywidgets import widgets
df = pd.DataFrame([[1,'car'],[2,'bus'],[3,'train']])
click_list = []
display(df[:1])
button = widgets.Button(description='click')
display(button)
def obc(b):
click_list.append((pd.to_datetime('now'),1))
button.on_click(obc)
button2 = widgets.Button(description='click')
display(button2)
def obc2(b):
click_list.append((pd.to_datetime('now'),2))
display(df[1:2])
button2.on_click(obc2)
button3 = widgets.Button(description='click')
display(button3)
def obc3(b):
click_list.append((pd.to_datetime('now'),3))
display(df[2:3])
button3.on_click(obc3)
which give out this:
it has the the basic functionality I'm after, but it is displayed in a confusing way (especially if we are talking about tens of rows) I would like to have something like this:
or similar, any help will be much appriciated
The simplest approach is to use a JavaScript library to add some interactivity to the DataFrame view in a notebook. The first one we will look at it Qgrid from Quantopian. This Jupyter notebook widget uses the SlickGrid component to add interactivity to your DataFrame.
In contrast, pandas + a Jupyter notebook offers a lot of programmatic power but limited abilities to graphically display and manipulate a DataFrame view. There are several tools in the Python ecosystem that are designed to fill this gap.
Often you may want to save a pandas DataFrame for later use without the hassle of importing the data again from a CSV file. The easiest way to do this is by using to_pickle () to save the DataFrame as a pickle file: This will save the DataFrame in your current working environment.
Here are few different approaches to create a hyperlink in Pandas DataFrame and JupyterLab: (1) Pandas method: to_html and parameter render_links=True: (2) Create clickable link from single column in Pandas DataFrame: In the next section, We'll review the steps to apply the above examples in practice.
It seems VBoxing html displays does a nice job (notice that IPython.display.HTML and ipywidgets.HTML are not the same)
import pandas as pd
from IPython.display import display, HTML
from ipywidgets import Button, HBox, VBox,widgets
import ipywidgets
df = pd.DataFrame([[1,'car'],[2,'bus'],[3,'train']])
click_list = []
button = widgets.Button(description='click')
def obc(b):
click_list.append((pd.to_datetime('now'),1))
button.on_click(obc)
button2 = widgets.Button(description='click')
def obc2(b):
click_list.append((pd.to_datetime('now'),2))
button2.on_click(obc2)
button3 = widgets.Button(description='click')
def obc3(b):
click_list.append((pd.to_datetime('now'),3))
button3.on_click(obc3)
display(HBox([VBox([widgets.Button(description=''),button,button2,button3]),ipywidgets.
HTML(df.style.set_table_attributes('class="table"').render())]))
Is there anyway to automate this? So it can be used with anunknown amount of checkboxes/rows? The code belows gives an errror: AttributeError: 'list' object has no attribute '_handle_displayed'
import pandas as pd
from IPython.display import display, HTML
from ipywidgets import Checkbox, HBox, VBox,widgets
import ipywidgets
df = pd.DataFrame(data=[['a',1],['b',32]], columns=['J1','J2'])
mydict = {}
t=0
for ts in df.J1:
mydict[str('c')+ str(t)] = widgets.Checkbox(value=False, description = 'Accepted')
t=t+1
display(HBox([VBox([widgets.Checkbox(description=''),mydict.values()]),ipywidgets.
HTML(df.style.set_table_attributes('class="table"').render())]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With