gpt4 book ai didi

Flask/React: Returned json data not being availabe(FlASK/REACTION:返回的json数据不可用)

转载 作者:bug小助手 更新时间:2023-10-27 20:09:50 26 4
gpt4 key购买 nike



I would like to transfer quite a big amount of data (ca. 1 MB) to the client to plot it in the browser. When I use port 5000 (Flask) I see all the returned data but as soon as I switch to 3000 (React) I only get the message that the received data is 0 (=pretset data in useState). What am I doing wrong?

我想传输大量的数据(CA)。1 MB)发送到客户端,以便在浏览器中绘制它。当我使用端口5000(Flask)时,我看到了所有返回的数据,但当我切换到3000(React)时,我只得到接收到的数据为0(= useState中的pretset数据)的消息。我做错了什么?


Python:

Python:


import pandas as pd
import time
import os
import json
import pandana
from pandana.loaders import osm
import warnings
import plotly
import plotly.express as px
import cartopy.crs as ccrs
from flask import Flask

app = Flask(__name__)

@app.route('/')
def map_nearest_pois():
... filling the (big) variable amenity_access ...

fig = px.scatter(amenity_access, x='longitude', y='latitude', color='distance_next_amenity')
graphJSON = plotly.io.to_json(fig, pretty=True)
return graphJSON

if __name__ == "__main__":
app.run(debug=True)

React:

反应:


import React, { useState, useEffect } from 'react'
import Plot from 'react-plotly.js'
import './App.css';

const App = () => {
const [plot, setPlot] = useState(0);

useEffect(() => {
const fetchData = async () => {
const data = await fetch('/')
.then(res => res.json())
.then(data => {setPlot(data);})
}

// call the function
fetchData()
.catch(console.error);
}, [])


return (
<div className="App">
<Plot data={plot.data} layout={plot.layout} />
</div>
);
}

export default App;

console.log shows me for "plot" just "0" and res.json() throws the then expected error that "0" is not json-formatted.
Where is the problem? Is it a time-dependent problem?

Console.log只向我显示了“Plot”的“0”,而res.json()抛出了“0”不是json格式的预期错误。问题出在哪里?这是一个依赖时间的问题吗?


更多回答

"console.log shows me for "plot" just "0" and res.json() throws the then expected error that "0" is not json-formatted." - It's not clear to me what you mean. Where in the code shown are you logging anything to the console? plot is initially set to 0, so it's expected to be 0 at some point. And res.json() isn't reading data from plot, so why would plot being 0 have any effect on that? Where is the port number used at all? What specifically is failing? In your browser's debugging tools, when the AJAX request is made, what is the server's response?

而res.json()抛出预期的错误:“0”不是json格式的。“--我不明白你的意思。在显示的代码中,您是否将任何内容记录到控制台?Plot最初设置为0,因此预计在某个点上为0。并且res.json()不会从Plot中读取数据,那么为什么Plot为0会对此产生任何影响呢?端口号到底在哪里使用?具体是什么失败了?在浏览器的调试工具中,当发出AJAX请求时,服务器的响应是什么?

优秀答案推荐
更多回答

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