This project demonstrates how to build a React frontend application that communicates with multiple backend services written in Python and R. It includes three examples:
- Python Calculator: A simple calculator using FastAPI (Python)
- R Statistics Calculator: A statistics analyzer using Plumber API (R)
- OpenAI Chat: A chat interface that connects to OpenAI's API
The frontend is built with React, TypeScript, and Vite, using Tailwind CSS for styling. It communicates with the backend services through HTTP requests.
The Python backend provides a simple calculator API with endpoints for basic arithmetic operations (addition, subtraction, multiplication, division).
The R backend provides a statistics API that calculates various metrics (mean, median, mode, etc.) for a list of numbers.
You'll need Node.js (v14 or newer) and npm installed:
-
Windows/macOS:
- Download the installer from Node.js official website
- Run the installer and follow the installation wizard
- Verify installation by running:
node --version npm --version
-
Linux (Ubuntu/Debian):
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - sudo apt-get install -y nodejs
-
Using NVM (Node Version Manager, recommended for developers):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash nvm install 16 nvm use 16
- Python 3.8+ for the Python backend
- R for the R backend
- OpenAI API key for the chat example
The project uses environment variables for configuration. An example file .env.example
is provided.
-
Copy the example file:
cp .env.example .env
-
Edit the
.env
file and add your OpenAI API key:VITE_OPENAI_API_KEY=your-openai-api-key
-
Navigate to the frontend directory:
cd frontend
-
Install dependencies:
npm install
-
Start the development server:
npm run dev
The React application will start on http://localhost:5173
-
Create and activate a conda environment:
conda create -n calculator-api python=3.10 conda activate calculator-api
-
Install dependencies:
cd backend/python pip install -r requirements.txt
-
Start the FastAPI server:
uvicorn calc_backend_api_endpoint:app --reload --port 8000
-
Create and activate a virtual environment:
cd backend/python python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Start the FastAPI server:
uvicorn calc_backend_api_endpoint:app --reload --port 8000
-
Open RStudio and install required packages:
install.packages(c("plumber", "jsonlite"))
-
Open the
backend/R/stats_backend_api_endpoint.R
file in RStudio -
Run the Plumber API server:
plumber::plumb("stats_backend_api_endpoint.R")$run(port=8080)
-
Install R from CRAN
-
Install required packages:
install.packages(c("plumber", "jsonlite"))
-
Start the Plumber API server:
cd backend/R Rscript -e "library(plumber); pr <- plumb('stats_backend_api_endpoint.R'); pr$run(port=8080)"
- Frontend Route:
/python-calculator
(i.ehttp://localhost:5173/python-calculator
) - Description: Performs basic arithmetic operations using a Python backend
- Requirements: Python backend must be running on port 8000
- Usage: Enter two numbers and select an operation to calculate the result
- Frontend Route:
/r-stats
(i.ehttp://localhost:5173/r-stats
) - Description: Calculates statistics for a list of numbers using an R backend
- Requirements: R backend must be running on port 8080
- Usage: Enter a comma-separated list of numbers to calculate statistics
- Frontend Route:
/chat
(i.ehttp://localhost:5173/chat
) - Description: Simple chat interface using OpenAI's API
- Requirements: Valid OpenAI API key in the
.env
file - Usage: Type messages to chat with the AI assistant
- Python Backend Issues: Check that FastAPI is running on port 8000 and CORS is properly configured
- R Backend Issues: Ensure the Plumber API is running on port 8080 and can parse JSON requests
- OpenAI API Issues: Verify your API key is correctly set in the
.env
file
├── frontend/ # React frontend application
| |.env.example # Example environment variables
│ ├── src/
│ │ ├── services/ # API clients for backend communication
│ │ └── pages/ # React components for each example
├── backend/
│ ├── python/ # Python FastAPI calculator backend
│ └── R/ # R Plumber statistics backend
└── README.md # Project documentation