How to host a Flask web server on Discord Bot hosting
What is flask? Flask is a web framework, it’s a Python module that lets you develop web applications easily. It’s has a small and easy-to-extend core. It does have many cool features like url routing, template engine. It is a WSGI web app framework. You can host a flask web server on sparked host! Let's find our how to do so
Installing Flask
- Head over to your service and click "Startup".
- Under the "Python Packages" box, add
flask
to the list of packages. Just like so:
python-dotenv
. Make sure to add this to the list of packages if you are using the example below.- Restart the service to install the package
Using Flask
Flask is easy to use and a lot of documentation is available for you to learn the ins and outs of the package. However, we will teach you how to setup a basic flask web server.
This is the example code that we have created that you can use. We are also using python-dotenv
in addition to Flask.
import os
from dotenv import load_dotenv
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "This is a flask web server hosted on Sparked Host!"
if __name__ == "__main__":
load_dotenv()
app.run(host='0.0.0.0', port=os.getenv('SERVER_PORT'))
Then create a file called .env
. The contents of the file should like this:
SERVER_PORT=25788
If you access the URL, the result should look something like this
If you require any further assistance, please create a ticket here.
Created By: Greg K
Updated on: 08/06/2023
Thank you!