Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use jupyter widgets to save clicks on a pandas dataframe

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:

enter image description here

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:

enter image description here

or similar, any help will be much appriciated

like image 276
Ezer K Avatar asked May 07 '17 14:05

Ezer K


People also ask

How to add interactivity to The Dataframe view in a Jupyter Notebook?

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.

What is the difference between pandas and Jupyter Notebook?

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.

How do I save a pandas Dataframe?

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.

How to create a hyperlink in pandas Dataframe and JupyterLab?

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.


2 Answers

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())]))

enter image description here

like image 70
Ezer K Avatar answered Nov 03 '22 08:11

Ezer K


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())]))
like image 31
user2866103 Avatar answered Nov 03 '22 07:11

user2866103