To add a "static" folder for your CSS and JavaScript files in your PythonAnywhere project, follow these steps:
Step 1: Create the Static Folder
-
Navigate to Your Project Directory:
Go to your project directory in PythonAnywhere, which is /home/nogra/mysite/.
-
Create a Static Folder:
Create a new folder named static in your project directory. Your directory structure should now look like this:
/home/nogra/mysite/
├── flask_app.py
├── bot_brains.py
├── templates/
│ └── index.html
└── static/
Step 2: Download the Required CSS and JavaScript Files
You will need to download the chessboard.js and chessboard.css files and place them in the static folder.
-
Download the Files:
You can download the files directly from the CDN or use the following links:
-
Upload the Files:
- Save the downloaded files to your local machine.
- In PythonAnywhere, go to the Files tab.
- Navigate to the
static folder you just created.
- Use the Upload a file option to upload
chessboard-1.0.0.min.js and chessboard-1.0.0.min.css to the static folder.
Step 3: Update Your HTML File
Now that you have the static files in place, you need to update your index.html file to reference these static files correctly.
- Edit
index.html: Open your index.html file in the templates folder and update it as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chess Game</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="/static/chessboard-1.0.0.min.js"></script>
<link rel="stylesheet" href="/static/chessboard-1.0.0.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.13.4/chess.js"></script>
<style>
#board {
width: 400px;
height: 400px;
margin: 20px auto;
}
</style>
</head>
<body>
<h1>Chess Game</h1>
<div id="board"></div>
<script>
// Initialize the chessboard
const board = Chessboard('board', {
position: 'start', // Start position
});
</script>
</body>
</html>
Step 4: Configure Static Files in PythonAnywhere
-
Go to the Web Tab:
In PythonAnywhere, navigate to the Web tab.
-
Add Static File Mapping:
Scroll down to the Static files section and add a new static file mapping:
- URL:
/static/
- Directory:
/home/nogra/mysite/static/
-
Reload Your Web App:
After making these changes, make sure to reload your web app by clicking the Reload button in the Web tab.
Step 5: Test the Application
- Open your web app URL (e.g.,
https://nogra.pythonanywhere.com/).
- You should now see the chessboard displayed correctly.
Final Directory Structure
After following these steps, your directory structure should look like this:
/home/nogra/mysite/
├── flask_app.py
├── bot_brains.py
├── templates/
│ └── index.html
└── static/
├── chessboard-1.0.0.min.js
└── chessboard-1.0.0.min.css
If you still encounter issues, please let me know, and we can troubleshoot further!