Forums

App only running on restart

I am new to pythonanywhere and flask and have recently got a flask web app running to communicate to Slack API. However, my app only appears to run in Slack directly after I press the "Reload gabarker02.pythonanywhere.com" button on the web tab, after which it does all the tasks it should have done since I last pressed it. The server log in between these times shows the app being accessed, and the only error message is OSError: write error. Any help would be much appreciated.

How do you connect do Slack, what triggers it? Is it being done in a view, or just as a call in your app? If the latter, it will be executed only once, when the web app is loaded.

Hi, thank you very much for your response. I am firstly initialising an app (called "app") from the slack_bolt module and writing some test functionality for it. I then initialize a flask app and register routes to the Flask app:

# Initialize Flask app
from flask import Flask, request
flask_app = Flask(__name__)
flask_app.config["DEBUG"] = True

# SlackRequestHandler translates WSGI requests to Bolt's interface
# and builds WSGI response from Bolt's response.
from slack_bolt.adapter.flask import SlackRequestHandler
handler = SlackRequestHandler(app)


# Register routes to Flask app
@flask_app.route("/slack/events", methods=["POST"])
def slack_events():
    # handler runs App's dispatch method
    return handler.handle(request)

flask_app is then imported into my wsgi configuration file as "application". Using my own pc, I require the line flask_app.run(debug=True) in my flask_app script to continuously run my app. Just wondering if this command can be implemented somewhere on my pythonanywhere web app, or if another solution may work?

Many thanks.

app.run is unnecessary on PythonAnywhere because that is for running the Flask dev server. Our servers execute your code without using the dev server.

Hi, thanks for your response. My file on pythonanywhere does not include the app.run line and has the original issue where it only runs when the web app is loaded. Any potential reasons or solutions to this would be much appreciated.

I have now solved this by removing the need for a slack_bolt app in my script - it now runs using just flask. Thanks for the help.

OK, glad to hear you got it working!