1

In a Jupyter notebook, how is it possible to display pandas cells displaying a clickable link allowing to trigger a call to a regular Python function?

For example, in the following dataframe, I would like to be able to manually click in a given row of the Process column to trigger the process_item(item) Python function:

   Item  Process
0  foo   click_here_to_process
1  bar   click_here_to_process

def process_item(item):
    ...
Xavier M
  • 547
  • 3
  • 6
  • 13

2 Answers2

1

I am not sure if it can be done interactive way, but here is way you can store and call functions with in a data frame.

import pandas as pd

def hello(parameter): # Some custom function 
    print(parameter)
    return

variable = hello # assign the function to a variable
data = {'col_1': [3, 2, 1, 0], 'col_2': [variable,variable,variable,variable]}
df = pd.DataFrame.from_dict(data)

df['col_2'][1]("hello World") #here we are calling the function with parameters
mohsinali
  • 286
  • 1
  • 9
0

Here is an example where each row in the dataframe has a clickable link which triggers a call to the regular python function select_row. The function is defined, currently, to set a global variable selection to the clicked row number, though the function can be defined differently.

To achieve this, the dataframe is embedded in custom html. I used this link for help with the Javascript.

import pandas as pd
from IPython.display import HTML as ipyhtml

# function for selecting a row
def select_row(row:'int'):
    global selection
    selection = row
    return

Defining the dataframe and interactivity in js + html

# define the dataframe
df = pd.DataFrame(data=dict(Name=['Mary', 'Bruce', 'Emma'], Score=[10,6,8]))

# add links as column into df, each with a unique id `mylink<row>`
# href = '#' does no redirecting in the browser
df['Select'] = [
    '<a id="mylink%s" href="#">Select</a>' % i for i in range(len(df))
]

# generate javascript <script>
js_script = """<script type="text/javascript">"""

for i in range(len(df)):
    js_script+="""
    
    // get a reference to each link "mylink<row>"
    
    var a%s = document.getElementById("mylink%s");
    
    // specify an "onclick" function for each link
    a%s.onclick = function() {
        // run the python function "select_row(<row>)"
        Jupyter.notebook.kernel.execute(`select_row(%s)`)
        return false;
      }
    """ % (i, i, i, i)
    
js_script+="""\n</script>"""


# generate the final html
selectable_df_html = """
    <html>
    <head>
    %s
    </head>
    <body>
    %s
    </body>
    </html>

""" % (js_script, df.style.to_html())

To display the df:

ipyhtml(selectable_df_html)

Clicking any row's "Select" button will set the global variable selection to that row number

  • Could you clarify how your answer addresses the question asked? What can one do with the `selection` global variable? Can one use your answer to "trigger a call to a regular Python function", as the OP wanted? – AlexK Jun 18 '22 at 04:29