A simple full-stack application that demonstrates basic API communication and translation features.
.
├── app/
│ ├── main.py # Flask backend
│ └── config.py # Configuration settings
├── ui/
│ ├── index.html # Frontend UI
│ ├── styles.css # Frontend styles
│ └── script.js # Frontend JavaScript
├── .env # Environment variables (create from .env.example)
├── .env.example # Example environment variables
└── requirements.txt # Python dependencies
- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Set up environment variables:
cp .env.example .envEdit .env and add your Google Translate API key.
- Run the Flask backend:
python app/main.pyThe backend will run on http://localhost:5010
To run the frontend separately:
- Navigate to the
uidirectory:
cd ui- Start a simple HTTP server:
python -m http.server 8080- Access the frontend at http://localhost:8080
When running frontend and backend on different ports:
- Update your
.envfile to include both origins:
TRUSTED_ORIGINS_DEVELOPMENT=http://localhost:5010,http://localhost:8080
-
The frontend JavaScript is configured to make requests to
http://localhost:5010/api/v1/greet -
If you change the backend port, update both:
- The port in
script.js - The port in your
.envfile
- The port in
- Start the backend (http://localhost:5010)
- Start the frontend (http://localhost:8080)
- Open http://localhost:8080 in your browser
- Enter a name (optional, defaults to "World")
- Select a language
- Click "Get Greeting"
Request body:
{
"name": "John", // optional, defaults to "World"
"language": "es" // optional, defaults to "en"
}Response:
{
"message": "¡Hola, John!"
}If you see CORS errors in the browser console:
- Check that both frontend and backend origins are in
TRUSTED_ORIGINS_DEVELOPMENTin.env - Ensure the backend is running on port 5010
- Ensure the frontend is running on port 8080
- Check that the fetch URL in
script.jsmatches your backend URL
If translation fails:
- Verify your Google Translate API key in
.env - Check the backend logs for specific error messages
- The backend runs in debug mode locally
- CORS is configured to allow requests from both frontend and backend origins
- Environment variables are loaded from
.env - Frontend and backend run on different ports for development
- Add error handling for API failures
- Implement frontend validation
- Add loading states and better error messages
- Set up automated testing