Fit table width to contents
parent
502769b76a
commit
3571d56c96
@ -1,164 +0,0 @@
|
||||
import json
|
||||
import numpy as np
|
||||
import dash
|
||||
import dash_table
|
||||
import pandas as pd
|
||||
import plotly.graph_objs as go
|
||||
import dash_table
|
||||
import dash_core_components as dcc
|
||||
import dash_html_components as html
|
||||
from dash.dependencies import Input, Output, State
|
||||
|
||||
app = dash.Dash(__name__)
|
||||
|
||||
app.layout = html.Div([
|
||||
html.Div(dcc.Graph(id='timeseries')),
|
||||
html.Div(id='div-table'),
|
||||
html.Div(id='datatable-row-ids-container'),
|
||||
html.Div(id='storage', style={'display': 'none'}),
|
||||
html.Div([
|
||||
html.Button('Load', id='button'),
|
||||
])
|
||||
])
|
||||
|
||||
# app.css.append_css('style.css')
|
||||
|
||||
|
||||
@app.callback(
|
||||
Output('storage', 'children'),
|
||||
[Input('button', 'n_clicks')],
|
||||
)
|
||||
def load_data(n_clicks):
|
||||
csv_name = 'test_20_WP.csv'
|
||||
|
||||
# Check instrument type
|
||||
inst_type = 'WP'
|
||||
if inst_type == 'WP':
|
||||
df = pd.read_csv(csv_name, index_col=0, header=5, skiprows=[6])
|
||||
|
||||
# Rename columns based on probe locations
|
||||
suffixes = ['P1', 'P2', 'P3', 'incident', 'reflected']
|
||||
col_names = list(df.columns)
|
||||
for i, col in enumerate(col_names[:-4]):
|
||||
if ('.' not in col) and (col_names[i + 4] == col + '.4'):
|
||||
for j, suffix in enumerate(suffixes):
|
||||
col_names[i + j] = '{}-{}'.format(col, suffix)
|
||||
df.columns = col_names
|
||||
else:
|
||||
df = pd.read_csv(csv_name, index_col=0, header=3, skiprows=[4])
|
||||
|
||||
# Zero time series based on first 5s
|
||||
df -= df[:5].mean()
|
||||
|
||||
df = df[:100]
|
||||
|
||||
# Round floats
|
||||
df = df.round(2)
|
||||
|
||||
return df.to_json(orient='table')
|
||||
|
||||
|
||||
@app.callback(
|
||||
Output('timeseries', 'figure'),
|
||||
[Input('storage', 'children')],
|
||||
)
|
||||
def update_graph(json_data):
|
||||
|
||||
if json_data is None:
|
||||
return {'data': []}
|
||||
|
||||
df = pd.read_json(json_data, orient='table')
|
||||
|
||||
ts = []
|
||||
for col in df.columns:
|
||||
trace = go.Scatter(x=df.index, y=df[col], name=col, opacity=0.8)
|
||||
ts.append(trace)
|
||||
|
||||
layout = {'title': 'basename', 'xaxis': {'rangeslider': {}}}
|
||||
timeseries = {'data': ts, 'layout': layout}
|
||||
|
||||
return timeseries
|
||||
|
||||
|
||||
@app.callback(
|
||||
Output('div-table', 'children'),
|
||||
[Input('storage', 'children')],
|
||||
)
|
||||
def update_table(json_data):
|
||||
if json_data is None:
|
||||
return {'data': []}
|
||||
|
||||
df = pd.read_json(json_data, orient='table')
|
||||
|
||||
stats = df.describe().T
|
||||
stats = stats.round(2)
|
||||
|
||||
s1 = stats.index.to_series()
|
||||
s1.name = 'Name'
|
||||
stats = pd.concat([s1, stats], axis=1)
|
||||
|
||||
datatable = dash_table.DataTable(
|
||||
id='datatable-row-ids',
|
||||
# columns=[{
|
||||
# 'name': [val, key],
|
||||
# 'id': key
|
||||
# } for key, val in variables.items()],
|
||||
columns=[{
|
||||
'name': x,
|
||||
'id': x
|
||||
} for x in stats.columns],
|
||||
data=stats.to_dict('records'),
|
||||
editable=False,
|
||||
sort_action='native',
|
||||
sort_mode='multi',
|
||||
row_selectable='multi',
|
||||
row_deletable=False,
|
||||
selected_rows=np.arange(stats.shape[0]),
|
||||
style_as_list_view=True,
|
||||
style_cell={
|
||||
'minWidth': '100px',
|
||||
'width': '100px',
|
||||
'maxWidth': '100px',
|
||||
},
|
||||
)
|
||||
return [datatable]
|
||||
|
||||
|
||||
# @app.callback(Output('datatable-row-ids-container', 'children'), [
|
||||
# Input('datatable-row-ids', 'derived_virtual_row_ids'),
|
||||
# Input('datatable-row-ids', 'selected_row_ids'),
|
||||
# Input('datatable-row-ids', 'active_cell')
|
||||
# ])
|
||||
# def update_graphs(row_ids, selected_row_ids, active_cell):
|
||||
# # When the table is first rendered, `derived_virtual_data` and
|
||||
# # `derived_virtual_selected_rows` will be `None`. This is due to an
|
||||
# # idiosyncracy in Dash (unsupplied properties are always None and Dash
|
||||
# # calls the dependent callbacks when the component is first rendered).
|
||||
# # So, if `rows` is `None`, then the component was just rendered
|
||||
# # and its value will be the same as the component's dataframe.
|
||||
# # Instead of setting `None` in here, you could also set
|
||||
# # `derived_virtual_data=df.to_rows('dict')` when you initialize
|
||||
# # the component.
|
||||
# selected_id_set = set(selected_row_ids or [])
|
||||
#
|
||||
# if selected_id_set:
|
||||
# dff = df.loc[selected_id_set]
|
||||
# else:
|
||||
# dff = df
|
||||
#
|
||||
# spec = []
|
||||
# for i, row in dff.iterrows():
|
||||
# x = np.arange(10)
|
||||
# y = x * row['H_sig']
|
||||
#
|
||||
# trace = go.Scatter(x=x, y=y, name=i)
|
||||
# spec.append(trace)
|
||||
#
|
||||
# energy = dict(data=spec)
|
||||
#
|
||||
# graph = dcc.Graph(id='time-series', figure=energy)
|
||||
#
|
||||
# return graph
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run_server(debug=True)
|
@ -1,101 +0,0 @@
|
||||
import os
|
||||
import dash
|
||||
from dash.dependencies import Input, Output
|
||||
import dash_table
|
||||
import dash_core_components as dcc
|
||||
import dash_html_components as html
|
||||
import plotly.graph_objs as go
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
# __file__ = '.'
|
||||
csv_name = os.path.dirname(__file__) + '../statistics.csv'
|
||||
df = pd.read_csv(csv_name)
|
||||
|
||||
df_spec = df.copy()
|
||||
df_spec.T
|
||||
|
||||
# Round floats
|
||||
df = df.round(2)
|
||||
|
||||
variables = {
|
||||
'location': '',
|
||||
'H_sig': 'o',
|
||||
'H_1%': 'x',
|
||||
'H_max': '□',
|
||||
'Hm0': '◊',
|
||||
'Tp': '',
|
||||
'Tp1': '',
|
||||
}
|
||||
|
||||
df = df[variables.keys()]
|
||||
|
||||
df['id'] = df['location']
|
||||
df = df.set_index('id', drop=False)
|
||||
|
||||
app = dash.Dash(__name__)
|
||||
|
||||
app.layout = html.Div([
|
||||
dash_table.DataTable(
|
||||
id='datatable-row-ids',
|
||||
columns=[{
|
||||
'name': [val, key],
|
||||
'id': key
|
||||
} for key, val in variables.items()],
|
||||
data=df.to_dict('records'),
|
||||
editable=False,
|
||||
sort_action='native',
|
||||
sort_mode='multi',
|
||||
row_selectable='multi',
|
||||
row_deletable=False,
|
||||
selected_rows=np.arange(df.shape[0]),
|
||||
style_as_list_view=True,
|
||||
style_cell={
|
||||
'minWidth': '100px',
|
||||
'width': '100px',
|
||||
'maxWidth': '100px',
|
||||
},
|
||||
),
|
||||
html.Div(id='datatable-row-ids-container')
|
||||
])
|
||||
|
||||
|
||||
@app.callback(Output('datatable-row-ids-container', 'children'), [
|
||||
Input('datatable-row-ids', 'derived_virtual_row_ids'),
|
||||
Input('datatable-row-ids', 'selected_row_ids'),
|
||||
Input('datatable-row-ids', 'active_cell')
|
||||
])
|
||||
def update_graphs(row_ids, selected_row_ids, active_cell):
|
||||
# When the table is first rendered, `derived_virtual_data` and
|
||||
# `derived_virtual_selected_rows` will be `None`. This is due to an
|
||||
# idiosyncracy in Dash (unsupplied properties are always None and Dash
|
||||
# calls the dependent callbacks when the component is first rendered).
|
||||
# So, if `rows` is `None`, then the component was just rendered
|
||||
# and its value will be the same as the component's dataframe.
|
||||
# Instead of setting `None` in here, you could also set
|
||||
# `derived_virtual_data=df.to_rows('dict')` when you initialize
|
||||
# the component.
|
||||
selected_id_set = set(selected_row_ids or [])
|
||||
|
||||
if selected_id_set:
|
||||
dff = df.loc[selected_id_set]
|
||||
else:
|
||||
dff = df
|
||||
|
||||
spec = []
|
||||
for i, row in dff.iterrows():
|
||||
x = np.arange(10)
|
||||
y = x * row['H_sig']
|
||||
|
||||
trace = go.Scatter(x=x, y=y, name=i)
|
||||
spec.append(trace)
|
||||
|
||||
energy = dict(data=spec)
|
||||
|
||||
graph = dcc.Graph(id='time-series', figure=energy)
|
||||
|
||||
return graph
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run_server(debug=False)
|
Loading…
Reference in New Issue