简体   繁体   English

如何连接flask rest API输出以做出反应并显示结果

[英]How to connect flask rest API output to react and display result

I am doing inference using flask rest API and I got the result我正在使用flask rest API 进行推理,我得到了结果

{
result: {
predictions: -3.4333341121673584
} }

bypassing multiple args in the get as the URL I got the above result http://127.0.0.1:3000/predict?solute=CC(C)(C)Br&solvent=CC(C)(C)O Now I want to use this result to use in a react app.绕过get中的多个args作为URL我得到了上面的结果http://127.0.0.1:3000/predict?solute=CC(C)(C)Br&solvent=CC(C)(C)O现在我想使用这个结果在反应应用程序中使用。

I have written the code below我写了下面的代码

import { useState, useEffect } from "react";

function App() {
  const [state, setState] = useState({});

  useEffect(() => {
    fetch("api/")
      .then((response) => {
        if (response.status == 200) {
          return response.json();
        }
      })
      .then((data) => console.log(data))
      .then((error) => console.log(error));
  });

I have written the following using a tutorial on the internet.我使用互联网上的教程编写了以下内容。 I am new to using fetch API or Axios.我是使用 fetch API 或 Axios 的新手。 Need help to get this result in react app需要帮助才能在反应应用程序中获得此结果

import { useState, useEffect } from "react";

function App() {
  const [data, setData] = useState(null);
  const [loading, setLoading = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    fetch("http://127.0.0.1:3000/predict?solute=CC(C)(C)Br&solvent=CC(C)(C)O")
      .then((response) => {
        if (response.status == 200) {
          return response.json();
        }
      })
      .then(setData)
      .catch(setError)
      .finally(() => setLoading(false));
  });

  if (loading) {
    return <p>Loading</p>
  }

  if (error) {
    return <p>{JSON.stringify(error)}</p>
  }

  return <p>{JSON.stringify(data)}</p>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM