Flask server in wasp

Hello everyone, in src/server i have put the file main.py that contains:
from flask import Flask
from richestBtc import richestBtc_bp

app = Flask(__name__)

app.register_blueprint(richestBtc_bp, url_prefix='/richestBtc')

if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=5000)
from flask import Flask
from richestBtc import richestBtc_bp

app = Flask(__name__)

app.register_blueprint(richestBtc_bp, url_prefix='/richestBtc')

if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=5000)
And by running python main.py everything works well. But how can run this in production? I use caddy at the moment:
datanext.app {
root * /var/www/html
encode gzip
try_files {path} /index.html
file_server
}

api.datanext.app {
reverse_proxy localhost:3001
}
datanext.app {
root * /var/www/html
encode gzip
try_files {path} /index.html
file_server
}

api.datanext.app {
reverse_proxy localhost:3001
}
Any idea on how running python main.py in prod? Thanks you
No description
No description
No description
No description
No description
No description
5 Replies
kapa.ai
kapa.ai3d ago
Hi @DanjelDurmo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To run your Flask application in a production environment, you can follow the advice given by Community Member 2 and Community Member 187 in the Discord support thread (https://discord.com/channels/686873244791210014/1204014940512452639). They suggest two approaches: 1. Run your Python program as a child process from your Wasp server/node code. You can use an action / query / job / api / whatever fits your use case. You can check this tutorial (https://www.digitalocean.com/community/tutorials/how-to-launch-child-processes-in-node-js) on how to launch child processes in Node.js for more details. 2. Deploy your Python program as a separate server. You can use Flask to wrap your Python code and serve it as an HTTP server. Then, you can send HTTP requests from Wasp server code to it. In your case, since you are already using Flask, the second approach might be more suitable. You would need to deploy your Flask application to a server, and then configure your Wasp application to send requests to the Flask server. As for your Caddy configuration, you would need to add a new block for your Flask application, similar to the one you have for your Wasp application. The reverse_proxy directive should point to the address and port where your Flask application is running. Please note that this is a high-level explanation and the exact steps might vary depending on your specific setup and requirements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @DanjelDurmo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
MEE6
MEE62d ago
Wohooo @bx2, you just became a Waspeteer level 2!
bx2
bx223h ago
you will need to run the flask via uvicorn or gunicorn or other wsgi server available out there. alternativelly, just use poco's recommendation described in their documentation: https://flask.palletsprojects.com/en/3.0.x/tutorial/deploy/#run-with-a-production-server
DanjelDurmo
DanjelDurmo23h ago
Thank you all, I have run successfully my flask backend. Very soon i will deliver my wasp webapp
martinsos
martinsos20h ago
Hey nice job @DanjelDurmo ! Btw here is a bit of what we have to say on Wasp + Python for now, including some options how to do it right now, and also a link to how one other user did it: https://github.com/wasp-lang/wasp/issues/1940 . How did you end up doing it?
GitHub
Add support for Python backend · Issue #1940 · wasp-lang/wasp
We had multiple people ask if they could use Python backend in Wasp. Mostly Flask was assumed, as a backend server. React as frontend is ok, even great. I will list here some ideas on how Python co...
Want results from more Discord servers?
Add your server