Liv*_*neM 6 python pandas plotly-dash
我正在使用 dash 应用程序调用包含 351 列的大约 250,000 个值的大型数据集,以便我可以显示它。然而,它需要很长时间才能运行,我认为这是因为我从一个不同的应用程序调用数据,我用来收集名为 REDCap 的数据。现在我想知道是否有更好的方法让我的应用程序运行得更快,即使数据来自不同的应用程序。请参阅下面的代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from redcap import Project
import pandas as pd
#redcap api and key
api_url = "enter link"
api_key = "enter key"
project = Project(api_url, api_key)
#call data from redcap
def data():
    df = project.export_records(format="df", df_kwargs={"index_col": project.field_names[1]})
    return df
df = data()
#generate table
def generate_table(dataframe, max_rows=10):
    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))]
    )
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(children=[
    html.H4(children='US Agriculture Exports (2011)'),
    generate_table(df)
])
if __name__ == '__main__':
    app.run_server(debug=True)
请协助我如何使应用程序运行得更快,因为我调用数据的部分正在减慢它的速度
这里有几件事:
1) 从 redcap 导出数据project.export_records可能是不必要的步骤。我不是 100% 确定您正在使用的数据结构,但我建议将对象转换为 Pandas 数据帧——Pandas 处理结构化数据的速度非常快。
2)假设您不打算显示所有数据,我建议将数据框的大小限制为所需的最小大小。
3) 为您的数据帧生成 html 的计算量很大并且有点循环,依赖于索引。我会对那里的代码进行以下更改:
# Generating the Body (Slightly more readable and a lot less loopy & indexy)
html_all_rows = []
for idx, row in dataframe[:max_rows].iterrows():
   html_row = html.Tr([html.Td(v) for v in row])
   html_all_rows.append(html_row)
4) 或者,我建议使用 Plotly 的内置datatable。它比典型的表格更具交互性+它允许真正整洁的排序和查询。数据表的数据输入是类似于字典的 json,因此一旦访问您的数据,速度就会提高。
5) 同样,我建议只将所需的数据加载到应用程序中。我无法想象 350 个字段对任何人都有用 - 同样,250,000 行。
| 归档时间: | 
 | 
| 查看次数: | 4304 次 | 
| 最近记录: |