gpt4 book ai didi

python - 如果 Dash 应用程序因导入的大数据而变慢,如何使 Dash 应用程序运行得更快

转载 作者:行者123 更新时间:2023-12-03 16:22:32 29 4
gpt4 key购买 nike

我正在使用 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) 使用 project.export_records 从 redcap 导出数据可能是不必要的步骤。我不是 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 行。

关于python - 如果 Dash 应用程序因导入的大数据而变慢,如何使 Dash 应用程序运行得更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317363/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com