Add new row in agr grid on click of a button

I want to append a row in existing ag grid with some default value (coming as list of list from DB) in Javascript. Can someone pl help me.

Hi @dssingh17

I came up with the following app that adds a new row to Aggrid upon click of a button. Here, we’re using Session State to allow the app to have memory of rows added to the DataFrame:


# Solution to

import streamlit as st
import numpy as np
import pandas as pd
from st_aggrid import AgGrid

# Set page width

# Load a CSV file
def gen_data():
    return pd.DataFrame(
        np.random.randn(4, 5),
        columns=('col %d' % i for i in range(5)))
df = gen_data()

# Initiate session state
if "data" not in st.session_state: = gen_data()

# Get input text values
new_row = st.text_input("Enter new row values separated by commas")
st.code('1, 2, 3, 4, 5')

# Convert input values to a list
new_row_list = new_row.split(',')

# Add a new row to the DataFrame after clicking a button
if st.button("Add row"):[len(] = new_row_list
    #df3 = pd.concat([df, df2], axis=1)
    # Display the DataFrame using AgGrid
    # Display the DataFrame using AgGrid

Demo of App

Screen Recording 2566-05-03 at 20_scaling-0.29_fps-20_speed-10.0_duration-0-25

Hope this helps!

Best regards,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.