Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding oncellclicked event using ipyaggrid #21

Open
taoranfeng opened this issue Feb 24, 2023 · 5 comments
Open

adding oncellclicked event using ipyaggrid #21

taoranfeng opened this issue Feb 24, 2023 · 5 comments

Comments

@taoranfeng
Copy link

Hi,

I'm trying to add an oncellclicked event to my table where the cell value is returned when user click the cell, I know this is possible using AG grid, just wondering is there a way to add this event using Python. Thanks!

@maartenbreddels
Copy link
Contributor

I don't know the answer to this specific question, did you check out the https://github.com/widgetti/ipyaggrid/tree/master/docs/notebooks directory. Maybe that helps you in the right direction

@vthemelis
Copy link
Contributor

You can add an even by adding a callback for onCellClicked in your grid options:

import ipyaggrid as ipg
import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35],
    'salary': [50000, 60000, 70000]
})

# Define the Ag-Grid column definitions
column_defs = [
    {'headerName': 'Name', 'field': 'name'},
    {'headerName': 'Age', 'field': 'age'},
    {'headerName': 'Salary', 'field': 'salary'},
]

# Define the Ag-Grid options
options = {
    'column_defs': column_defs,
    'enableSorting': True,
    'enableFilter': True,
    'enableColResize': True,
    'onCellClicked': 'function(params) {alert(params.value)}'
}

# Create the Ag-Grid widget
grid_widget = ipg.Grid(
    grid_data=df,
    grid_options=options
)

# Display the widget
grid_widget

@maartenbreddels
Copy link
Contributor

Thanks @vthemelis
do you know how to trigger a python callback using this? that would be a really nice example I think.

@vthemelis
Copy link
Contributor

vthemelis commented Mar 28, 2023

do you know how to trigger a python callback using this? that would be a really nice example I think.

I don't think that this is something that is possible to do right-off-the-box in ipyaggrid. You would probably need some sort of IPC framework for this. Maybe this: https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Low%20Level.html#comms ?

@mariobuikhuizen
Copy link
Contributor

Here's a workaround using ipyvue:

import ipyaggrid as ipg
import pandas as pd
import ipyvue
import traitlets

class MyEvents(ipyvue.VueTemplate):

    @traitlets.default("template")
    def _template(self):
        return """
        <script>
        module.exports = {
          created() {
            window.myNamespace = {
              passEvent: (params) => {
                   this.my_message(params) 
              },
            }
          }
        }
        </script>
        """
    
    def vue_my_message(self, data):
        print("data: ", data)
        
display(MyEvents())


# Create a sample dataframe
df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35],
    'salary': [50000, 60000, 70000]
})

# Define the Ag-Grid column definitions
column_defs = [
    {'headerName': 'Name', 'field': 'name'},
    {'headerName': 'Age', 'field': 'age'},
    {'headerName': 'Salary', 'field': 'salary'},
]

# Define the Ag-Grid options
options = {
    'column_defs': column_defs,
    'enableSorting': True,
    'enableFilter': True,
    'enableColResize': True,
    'onCellClicked': '''function(params) {
        console.log("params: ", params);
        const { rowIndex, type, data } = params;
        myNamespace.passEvent({ rowIndex, type, data });
    }'''
}

# Create the Ag-Grid widget
grid_widget = ipg.Grid(
    grid_data=df,
    grid_options=options
)

# grid_widget.on_msg(lambda *args: print("onmsg", args))

# Display the widget
grid_widget

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants