1

I am currently working on a small project to build a web application to visualize some local data produced by some Python scripts I have already finished, and the front-end data visualization is written in react. I am kinda confused how could I fetch the data to represent on my web app. I saw some suggestion of using fetch() instead of jQuery. But everything I have done yet is just locally hosted, so how could I make the request to Python scripts in React and how should I retrieve params from the fetch() request in Python? Thank you!

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import RadarChart from './components/RadarChart';
import LineChart from './components/LineChart';

class App extends Component {
  constructor() {
    super();
    this.state = {
      bioData: {},
      appData: {}
    }
  }

  componentWillMount() {
    this.getChartData();
  }
  
  static lastSyncTime = "2017-06-28";
  getChartData() {
    // TODO: AJAX calls/fetch() request here to retrieve data as well as pass a param: lastTimeSync to Python scripts
    this.setState({
      
    });
  }

  
  

  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>Daily Report</h2>
        </div>
        <LineChart  chartData={this.state.bioData} />
        <br/>
        <button onClick = {this.handleRefreshLine}>Refresh</button>
        <br/>
        <RadarChart chartData={this.state.appData} />
      </div>
    );
  }
}

export default App;

2
  • 1
    You'll need to expose an api from your python backend. You'll need to make an endpoint that accepts params and returns your data. If using fetch (or axios), you'll then update your state with the response from your backend. Too broad of a question to give specific code examples though. Commented Jul 6, 2017 at 22:04
  • @ChaseDeAnda thx man. I am just looking for a direction, not exactly the code. I'll try your suggestion! Commented Jul 7, 2017 at 4:31

1 Answer 1

1

Something like this may help you:

getChartData() {
  $.getJSON("https://yourserver.com/your/python/api", { lastTimeSync: App.lastSyncTime }).then(results => {
    this.setState({ appData: results.appData, bioData: results.bioData })
  })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.