Skip to content

shiny widget and itable problem (table not extending to show all rows) #360

@danieltomasz

Description

@danieltomasz

I have my MVE code here

import pandas as pd
import numpy as np
from shiny import App, ui, render, reactive
from shinywidgets import output_widget, render_widget, reactive_read
from itables.widget import ITable

# Generate sample data with 10 rows
np.random.seed(42)  # For reproducibility
descriptions = [
    "This is a very long description that needs to wrap across multiple lines to be readable in the table.",
    "Another long text that demonstrates the need for proper column width settings and text wrapping capabilities.",
    "The third description with enough content to show how text wrapping works in DataTables with proper configuration.",
    "When setting up tables with long text content, it's important to configure the column widths appropriately.",
    "Text wrapping is essential for maintaining readability while efficiently using the available screen space.",
]

# Create a DataFrame with 10 rows
data = []
for i in range(1, 11):
    data.append(
        {
            "ID": i,
            "Title": f"Item {i}",
            "Category": np.random.choice(["Product", "Service", "Resource", "Tool"]),
            "Price": round(np.random.uniform(10, 1000), 2),
            "Description": descriptions[i % len(descriptions)],
        }
    )

df = pd.DataFrame(data)

# Define UI
app_ui = ui.page_fluid(
    ui.h1("ITables Widget Example"),
    ui.p("This example shows the use of ITable widget with Shiny output_widget"),
    # Add custom CSS for styling the table
    ui.HTML("""
    <style>
        /* Control text wrapping */
        .dataTable td {
            white-space: normal !important;
            word-wrap: break-word;
            max-width: 0;  /* Important for text wrapping */
        }
        
        /* Improve table appearance */
        .dataTable {
            border-collapse: collapse;
            width: 100%;
        }
        
        .dataTable th, .dataTable td {
            padding: 8px;
            border: 1px solid #ddd;
        }
        
        /* Optional: Add some hover effect */
        .dataTable tr:hover {
            background-color: #f5f5f5;
        }
    </style>"""),
    # Add a dropdown to select rows
    ui.input_select(
        "row_selection",
        "Select rows:",
        {"none": "None", "even": "Even rows", "odd": "Odd rows", "all": "All rows"},
    ),
    # Output widget placeholder
    output_widget("my_table"),
    # Display selected rows
    ui.output_text("selected_rows_info"),
)


# Define server
def server(input, output, session):
    @render_widget
    def my_table():
        return ITable(
            df,
            caption="Sample Data with ITable Widget",
            select=True,
            columnDefs=[
                {"width": "5%", "targets": 0},
                {"width": "15%", "targets": 1},
                {"width": "10%", "targets": 2},
                {"width": "10%", "targets": 3},
                {"width": "60%", "targets": 4},
            ],
            style="table-layout:fixed; width:100%;",
        )

    @reactive.effect
    def _():
        # Update the widget based on selection
        selection = input.row_selection()

        if selection == "none":
            selected_rows = []
        elif selection == "even":
            selected_rows = [i for i in range(len(df)) if (i + 1) % 2 == 0]
        elif selection == "odd":
            selected_rows = [i for i in range(len(df)) if (i + 1) % 2 == 1]
        else:  # all
            selected_rows = list(range(len(df)))

        my_table.widget.update(selected_rows=selected_rows)

    @render.text
    def selected_rows_info():
        rows = reactive_read(my_table.widget, "selected_rows")
        if not rows:
            return "No rows selected"
        return f"Selected rows: {rows}"


# Create the Shiny app
app = App(app_ui, server)

the rows at the bottom are cut
Image

my itables are 2.2.5, shiny is 1.3 and shinywigets 0.5.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions