This is not a complete tutorial on either React or Flask but simply a detailed example on how they can be used together.
When starting a new React application, the recommended way is to use the Create React App CLI tool. Of course, you'll be expected to have the latest versions of Node and Npm (which is included by default in the Node installer for Windows) from https://nodejs.org/en/.
To use the Create React App CLI, you can run the following command: npx create-react-app react-demo
Here, react-demo is the name of our application.
This operation creates a folder called react-demo wherever you ran this command. It also automatically runs npm install along with the required packages. Finally, it creates some other basic files so that you can directly try running the application and see if it works!
To run the application, first navigate to the new folder using: cd react-demo
Then, run: npm start
To start designing your application, you can wipe the content present in ./src/App.css and ./src/App.js and write your own code!
To use Bootstrap, you'll need to first install jquery by running: npm install jquery.
You can add Bootstrap to your application by running npm install bootstrap and then adding the following to the start of the index.js file:
import 'bootstrap/dist/css/bootstrap.css';
import 'jquery/dist/jquery.js';
import 'bootstrap/dist/js/bootstrap.bundle.js';The Create React App tool doesn't come with the React Router package by default. To add it, run:
npm install react-router-dom
You can then follow the basic example given here: https://reactrouter.com/web/example/basic to familiarize yourself with the components used for navigation to work. In this application, we have four routes, two for login / logout, another for registration and finally one for a dummy dashboard.
The Create React App tool doesn't specify any specific folder structure to follow. In this application, I've created a components folder within the src folder where I will include files for my components as required. It doesn't really matter in this application because of its size, however if your application is complex, deciding on a good folder structure before starting is recommended.
Vanilla JavaScript provides us with two ways to make HTTP requests - the XMLHttpRequest Object and the Fetch API. Both however, are lower level ways to do AJAX requests and are meant to be abstracted using a custom function anyway. So we'll be using a library called Axios that does just this and much more. To install axios, run npm install axios.
Since AJAX requests don't give any feedback to the user whether they succeeded or failed automatically, it is upto the developer to do so. You can use react-toastify for this. To install it, run npm install react-toastify. You can learn how to use it at https://fkhadra.github.io/react-toastify/introduction/.
We can add route guards using react-router-guards to ensure that only logged-in users can access pages that require authorization. You can install it using npm install react-router-guards and find its documentation at https://www.npmjs.com/package/react-router-guards.
As Flask is a microframework, using it is very straightforward. Simply make sure that you have the latest version of Python 3 installed from https://www.python.org/ and follow these steps:
- Create a folder named
flask-demo. - Create a virtual environment using:
python -m venv venv. - On Windows, run
venv\Scripts\activateto enter the virtual environment. (This step is required everytime you want to run your application). - Install Flask using
pip install Flask. - Create a file named
app.pyin theflask-demofolder. - Add the following minimal code in it:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'- Finally, run
flask runto run your flask application!
CORS would be an issue for us since our client and API applications are running on different hostnames. To bypass this issue, we can use the flask-cors package by running pip install flask-cors and including it in our application. To do so, you can follow the example on their package web page at https://pypi.org/project/Flask-Cors/.
As quoted from the Tinydb package page at https://pypi.org/project/tinydb/:
TinyDB is a lightweight document oriented database optimized for your happiness :) It’s written in pure Python and has no external dependencies. The target are small apps that would be blown away by a SQL-DB or an external database server.
Important Note: Tinydb is not meant to be used for a HTTP server as it does not provide ACID guarantees. It is only being used here for example purposes!
To install tinydb, first make sure that your virtual environment is running, then run: pip install tinydb. After that, you should be able to use tinydb as usual in your app.py file.
You can find the documentation for tinydb at https://tinydb.readthedocs.io/en/latest/.
Using the bcrypt library, we can safely store our passwords in our database. To install bcrypt, run pip install bcrypt.
To install PyJWT, run: pip install PyJWT. You can look up its usage at https://github.com/jpadilla/pyjwt.