Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html dash table

I am trying to use dash to create a html table

My dataframe looks like this :

   Cap non-cap
0   A   a
1   B   b
2   C   c
3   D   d
4   E   e
..
26  Z   z

I want to display an html table just like the dataframe, but without the 0 - 26 index. The structure is

{'Cap' : ['A', 'B', 'C',....], 'non-Cap' : ['a','b','c',...]}

I tried :

return html.Table(
  [html.Tr([html.Th(col) for col in dataframe.columns])] +
  [html.Tr([
    html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
  ]) for i in range(min(len(dataframe), max_rows))]  
)
like image 584
Matt-pow Avatar asked Sep 07 '18 00:09

Matt-pow


People also ask

What is a dash table?

Dash DataTable is an interactive table component designed for viewing, editing, and exploring large datasets. This component was written from scratch in React. js specifically for the Dash community. Its API was designed to be ergonomic and its behavior is completely customizable through its properties.

What is Dash HTML?

Dash is a web application framework that provides pure R and Python abstraction around HTML, CSS, and JavaScript. Instead of writing HTML or using an HTML templating engine, you compose your layout using R functions within the dashHtmlComponents package.

What is Dash app layout?

The layout of a Dash app describes what the app looks like. The layout is a hierarchical tree of components. Dash HTML Components ( dash. html ) provides classes for all of the HTML tags and the keyword arguments describe the HTML attributes like style , class , and id .

Is CSS a dash component?

Dash's component libraries, like dash_core_components and dash_html_components , are bundled with JavaScript and CSS files. Dash automatically checks with component libraries are being used in your application and will automatically serve these files in order to render the application.


1 Answers

import dash
import dash_html_components as html
import pandas as pd

data = {'Cap' : ['A', 'B', 'C', ], 'non-Cap' : ['a','b','c', ]}
df = pd.DataFrame(data)

def generate_table(dataframe, max_rows=26):
    return html.Table(
        # Header
        [html.Tr([html.Th(col) for col in dataframe.columns]) ] +
        # Body
        [html.Tr([
            html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
        ]) for i in range(min(len(dataframe), max_rows))]
    )

app = dash.Dash(__name__, )

app.layout = html.Div(children=[
    html.H4(children='StackOverflow - Html dash table'),
    generate_table(df)
])

if __name__ == '__main__':
    app.run_server(debug=True)
like image 174
user3049953 Avatar answered Oct 03 '22 07:10

user3049953