0

How do I create a website with a React Front end and a Flask backend?

I have created websites using flask and templates, and I have made pages using react, and now I would like to combine them.

I have tried a few things and the only things that worked required going into react config files and were very complicated. And even then it was complicated to use fetch and I had to run npm run build every time I changed the react file.

This seems to me like something that would be done all of the time yet I can't find any simple resources to do this.

Is there something that I fundamentally don't understand regarding websites and I am going at this the wrong way?

  • Lots of ways to approach this depending on a variety of factors. Are you running locally or deploying to the web? Managed platform (GAE, Heroku) or bare-bones VM? Containers? The best answer will be a function of your use case. – vulpxn May 05 '20 at 19:29
  • Literally all I want is to build a simple flask app but instead of rendering templates, I want to render React pages, and I want the React to be able to use fetch("/API") to get info from the server, I haven't worked with all those complicated things you mentioned so I don't really need those – Noe Horowitz May 06 '20 at 09:59

1 Answers1

1

Focusing on a development workflow, as there are countless choices in how to deploy to a production environment.

Run your Flask app with /api as the root url prefix, either manually by prefixing all your route decorators or with the use of a Blueprint.

py/app.py

@app.route('/api')
def index():
    return {'message':'hello'}

Add the Flask development server url to your package.json file

js/package.json

{
    ...,
    "proxy": "http://localhost:5000"
}

Fetch your data using a component

js/Flask.js

import React, { Component } from 'react'

export class Flask extends Component {
    constructor(props) {
        super(props)
        this.state = { data: {}, status: null }
    }

    fetchData() {
        let status
        fetch('/api')
        .then((res) => {
            return {
                data: res.json(),
                status: status
            }
        })
        .then((data) => {
            this.setState({ ...data })
        }
        .catch((err) => {
            console.error(err)
        })
    }

    componentDidMount() {
        this.fetchData()
    }

    render() {
        const { data, status } = this.state
        return (
            <div>
                <h3>Status: { status }</h3>
                <pre>
                    { JSON.stringify(data) }
                </pre>
            </div>
        )
    }
}

export default Flask

Finally, include the component in your main App

js/App.js

import React from 'react';
import Flask from './Flask'

function App() {
  return (
    <div className="App">
      <Flask />
    </div>
  );
}

export default App;

Start your Flask server with your preferred method, either flask run or by executing your script directly, then start your JS development server with either yarn or npm start. You should see the response from your api route displayed at http://localhost:8000

As long as you are running your Flask server with debug=True and use npm start (not npm run build), any changes made with either the backend or frontend will be automatically detected and your app reloaded.

vulpxn
  • 731
  • 1
  • 6
  • 14