【问题标题】:How to print what’s returned in a flask route in react native [duplicate]如何在本机反应中打印烧瓶路线中返回的内容[重复]
【发布时间】:2020-11-18 19:59:51
【问题描述】:

我有一个项目,关于使用烧瓶在 React Native 中运行 ML 模型并将预测结果发送到 React Native 应用程序。

现在,我有这个代码:

@app.route('/')
def home():
    return 'Hello, World!'


@app.route('/predict/<valueOne>,<valueTwo>')
def predict(valueOne, valueTwo):

    prediction = model.predict([[valueOne, valueTwo]])
    output = round(prediction[0], 2)

    return 'Sales should be $ {}'.format(output)

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

如果我打开浏览器并运行http://127.0.0.1:5000/predict/2,3,它将如下所示:

我想在我的 react 本机控制台上打印值 (650.0)。我尝试使用fetch,但没有奏效。我该怎么做?

编辑: 这是我尝试使用 fetch 的方法:

console.log(fetch('http://127.0.0.1:5000/predict/2,3'))

输出是:

Promise {
    "_40": 0,
    "_55": null,
    "_65": 0,
    "_72": null,
}

【问题讨论】:

  • 您是否在 fetch 中遇到任何错误?
  • @GuruparanGiritharan 嘿,嗯,我没有收到任何错误,但我不知道如何使用 fetch 获取值,我的一位朋友告诉我,您只能使用 fetch 来检索json 数组,对吗?
  • 不,你可以检索任何东西检查这个答案在得到一个字符串stackoverflow.com/questions/45427386/…
  • @GuruparanGiritharan 是的,我刚刚试了一下,效果很好

标签: python react-native flask


【解决方案1】:

我有这方面的经验。如果您还没有,则需要发出 axios 请求并更改状态。它应该看起来像这样:

import axios from 'axios';

constructor(props){
 
 this.state = {
        data: []
    };
}

async componentDidMount() {

    axios.get(api_url_card)
        .then(res => {
            const data = res.data;
            console.log(data)
            this.setState({ data : data });
    });
}

上面的axios请求的console.log会打印出请求的数据。但是,此代码还应该帮助您保存状态。希望这会有所帮助,祝你好运。

【讨论】:

  • 如果您愿意,还可以在请求中添加错误处理。
猜你喜欢
  • 2018-04-28
  • 2017-09-03
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 2012-01-28
相关资源
最近更新 更多