1

I am trying to build a data table that includes a checkbox in the first column using python reflex to build web app front end interface.

enter image description here

This is an example table obtained with:

import reflex as rx

def index() -> rx.Component:

return  rx.data_table(
        data    = data,
        columns = ["A", "B", "C"],
        pagination=True,
        search=True,
        sort=True,
    )

I would like to add a checkbox as data_table first column. Unfortunatley when I try this it tells me that It cannot recognise data type, maybe data_table element cannot contain any checkbox element.

I thus tried to implement it using reflex.html and reflex.script that should include the possibility to write a native code in HTML and JS inside the code:

    <!DOCTYPE html>
<html>
<head>
    <title>Data Table with Checkboxes</title>
</head>
<body>
    <h1>Data Table with Checkboxes</h1>
<table id="data-table">
    <thead>
        <tr>
            <th>Select</th>
            <th>Key</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody id="table-body">
    </tbody>
</table>

<script>
    // Sample dictionary 
    var data = {
        "item1": "Value 1",
        "item2": "Value 2",
        "item3": "Value 3",
        // Add more key-value pairs as needed
    };

    // Get the table body element
    var tableBody = document.getElementById("table-body");

    // Loop through the dictionary and create rows with checkboxes
    for (var key in data) {
        if (data.hasOwnProperty(key)) {
            var row = document.createElement("tr");

            // Create a checkbox in the first column
            var checkboxCell = document.createElement("td");
            var checkbox = document.createElement("input");
            checkbox.type = "checkbox";
            checkboxCell.appendChild(checkbox);
            row.appendChild(checkboxCell);

            // Create cells for key and value
            var keyCell = document.createElement("td");
            keyCell.textContent = key;
            row.appendChild(keyCell);

            var valueCell = document.createElement("td");
            valueCell.textContent = data[key];
            row.appendChild(valueCell);

            // Append the row to the table body
            tableBody.appendChild(row);
        }
    }
</script>

Obtaining this:

enter image description here

I do not know how to fill this table with custom data, How can I communicate between front end and back end using reflex python ?

0

Browse other questions tagged or ask your own question.