python - 实时从 django 获取数据到 React
问题描述
我创建了一个 API,它使用 python 请求库从 jenkins 下载工件。API 由 react 前端触发。我想在反应前端显示下载进度。
download_folder = "location"
bytes_read = 0
url = 'https://jenkinsServer/job/'+ ID + '/' + Build_no + '/artifact/*zip*/archive.zip'
response = requests.get(url, auth=(username, APIPin), stream=True, allow_redirects=True, verify = False)
with open(download_folder, 'wb') as f:
itrcount=1
for chunk in response.iter_content(chunk_size=1024):
itrcount=itrcount+1
f.write(chunk)
bytes_read += len(chunk)
print(bytes_read)
response.close()
上面是 Django 内部的视图。我想将 bytes_read 值获取到前端反应以将其用于进度条。在反应中,我只是使用了 fetch
var url = "DJANGO API URL FOR THE ABOVE CODE;
fetch(url)
.then(res => res.json())
.then()
.catch((error) =>{ console.log('Error');
})
我发现了一些可以在 Django 中用于发送实时数据的通道,但不太了解。我是 Django 和 React 的初学者,因此请不要介意代码/任何错误。我只需要在前端显示 python 请求的下载进度。或者如果我可以使用任何其他方法?
解决方案
推荐阅读
- excel - 从 Excel 保存多个 PDF 文件
- kotlin - 在泛型中使用 Nothing 是一个好习惯吗?
- javascript - 如何针对不同的数组转换或排序数组?
- r - 仅在单击菜单项时加载闪亮模块
- sap - SAPJco 调用 BAPI_MATERIAL_DISPLAY
- python - 大熊猫整数到日期时间索引的多索引?
- scala - 在 Typesafe Config 中处理环境变量
- lua - 为什么 Lua loadstring() 在演示站点上不起作用?
- c++ - 在 C++ 项目中使用 librdkafka (VS2013)
- chatbot - 与 slack 集成时,RASA 聊天机器人无法正常工作