Ive been trying to get flask working in thorny to connect to my website and no amount of ai help is working. I have an issue where thonny seems to be running it fine but when i go to the address they just load indefinitely would really appreciate some help
You're vague as to what platform you are running on. I will assume Rpi5 for an example --
The following answer is from GROK on X:
Output:
To run a Flask application using the Thonny IDE on a Raspberry Pi 5 with Python, follow these steps. Since Thonny comes pre-installed with Raspberry Pi OS, it’s a convenient choice for beginners and seasoned developers alike. Below is a step-by-step guide:
Step 1: Set Up Your Raspberry Pi
Ensure Raspberry Pi OS is Installed: Make sure your Raspberry Pi 5 is running the latest version of Raspberry Pi OS (Bookworm recommended as of March 2025). You can update it via the terminal:
bash
sudo apt update
sudo apt upgrade
Check Python Version: Thonny uses Python 3 by default. Verify it’s installed:
bash
python3 --version
As of March 2025, Python 3.11 or later should be pre-installed with Raspberry Pi OS.
Step 2: Install Flask
Open a Terminal: You can do this directly on the Raspberry Pi or via SSH.
Install Flask: Use pip to install Flask:
bash
pip3 install flask
Verify Installation: Check that Flask is installed:
bash
pip3 show flask
Step 3: Create a Simple Flask Application
Open Thonny: On the Raspberry Pi desktop, go to Menu > Programming > Thonny Python IDE.
Create a New File: In Thonny, click File > New.
Write a Basic Flask App: Enter the following code:
python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, Raspberry Pi 5!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
host='0.0.0.0': Makes the app accessible over your local network.
port=5000: Default Flask port.
debug=True: Enables debug mode (useful for development, but disable for production).
Save the File: Click File > Save As, name it app.py, and save it in a convenient location (e.g., /home/pi/).
Step 4: Run the Flask Application in Thonny
Run the Code: In Thonny, click the green Run button (▶) or press F5.
Check Output: Thonny’s shell will display something like:
* Running on http://0.0.0.0:5000
* Debug mode: on
If there are errors, Thonny will highlight them in the code and shell.
Test the App: Open a web browser on the Raspberry Pi or another device on the same network. Enter the Pi’s IP address followed by :5000 (e.g., http://192.168.1.100:5000). To find your Pi’s IP:
bash
hostname -I
You should see "Hello, Raspberry Pi 5!" displayed.
Step 5: Troubleshooting
Port Conflict: If port 5000 is in use, change the port value in app.run() (e.g., port=5001).
Firewall: Ensure the Raspberry Pi’s firewall allows traffic on port 5000:
bash
sudo ufw allow 5000
Network Access: If the app isn’t accessible from other devices, verify host='0.0.0.0' is set and check your network settings.
Step 6: Enhance Your Flask App (Optional)
Add more routes, e.g.:
python
@app.route('/about')
def about():
return "This is a Flask app on Raspberry Pi 5!"
Use Thonny’s debugger (Debug button or F6) to step through code if needed.
Notes
Thonny Limitations: Thonny is great for simple projects but may feel basic for complex Flask apps. For larger projects, consider using a terminal or another IDE like VS Code (via SSH).
Performance: The Raspberry Pi 5 is powerful, but Flask’s built-in server is for development. For production, use a WSGI server like Gunicorn:
bash
pip3 install gunicorn
gunicorn --bind 0.0.0.0:5000 app:app
That’s it! You now have a Flask application running on your Raspberry Pi 5 using Thonny. Let me know if you need help expanding it further!
Can you access the page generated by Flask if you run the Python Script outside Thonny by going to the directory the file is stored at and execute python test.py
? Which OS are you operating on?
Regards, noisefloor
(Mar-15-2025, 12:52 PM)Confusednoob Wrote: [ -> ]Ive been trying to get flask working in thorny to connect to my website and no amount of ai help is working
When using Flask or any web framework you,run it from command line never in Editor/Ide.
Editor/Ide can block or is not meant to run stuff like this,when run eg
python test.py
,
it will start Flask
build in webserver
and give you a adress to paste in Browser.
Running your code.
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5002, debug=True)
On command line.
(flask_app) C:\code\flask_app> python test.py
* Serving Flask app 'test'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5002
* Running on http://192.168.1.162:5002
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 204-399-216
127.0.0.1 - - [16/Mar/2025 18:43:47] "GET / HTTP/1.1" 200 -
Or you can delete line 11-12 and use
flask run
to start the app.
(flask_app) C:\code\flask_app> flask --app test run
* Serving Flask app 'test'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
127.0.0.1 - - [16/Mar/2025 18:48:57] "GET / HTTP/1.1" 200 -
Both work and show
Hello World
in Browser.