【发布时间】:2021-04-12 16:29:53
【问题描述】:
目前,我的应用程序是选择股票数据,然后使用另一个 python 脚本对其进行分析并输出结果(以 JSON 格式)。 现在我想添加一个按钮来将结果(w_df_split)输出到 CSV,然后当用户单击按钮并下载 csv 文件时。 但我被困在如何在这个视图中返回输出 CSV 函数。
views.py:
def efficient_frontier_select(request):
user_holding = Position.objects.filter(created_by = request.user)
selected_stock = None
w_df_split = None
if request.method == 'POST':
selected_stock = request.POST.getlist('stock_symbol')
ta, tb, tc = ef.combination_frontier(selected_stock)
fy_df, fx_df, w_df, fxfy_df, ef_df = ef.transform_frontier_todf(ta,tb,tc, selected_stock)
w_df_split = json.loads(ef.json_format_split(w_df))
context = {
'w_df_split' : w_df_split,
}
return render(request, 'portfolio/efficient_frontier.html', context)
【问题讨论】:
-
这能回答你的问题吗? Save results to csv file with Python
-
我编辑了..抱歉这个不清楚的问题..现在我想添加一个按钮来将结果(w_df_split)输出到 CSV,然后当用户单击按钮并下载 csv 文件时.但我被困在如何在这个视图中返回输出 CSV 函数。