1

I have a flask app running, I want something what can interacts with it. Let me explain it with the next example:

Flask app:

from flask import Flask

MyApp = Flask(__name__)

mytext = "Hello World!"

@app.route('/')
def hello_world():
    return mytext

def text_reload():
    global mytext
    mytext = "Hey! I have been reloaded!"

if __name__ == '__main__':
    MyApp.run()

Flask-Script (manager.py):

from flask.ext.script import Manager
from myapp import MyApp

manager = Manager(MyApp)

@manager.command
def reload_mytext():
    **DO SOME MAGIC HERE**

if __name__ == '__main__':
    manager.run()

That reload_mytext() calls text_reload() function from within the app.

What magic should I put in flask-script to accomplish this stuff? Is such a thing even possible?

KennyPowers
  • 4,925
  • 8
  • 36
  • 51
  • Flask-Script runs in a new process each time you use the command-line; this process is always going to be a separate one from the running server. This means you need some form of interprocess communication, or a shared external database, to achieve what you want to do. What is the goal here, what are you trying to achieve, ultimately? – Martijn Pieters Apr 14 '14 at 15:55
  • I want to create a flask-script scenario which can be called when its needed to make flask app to reload/redefine some variables. – KennyPowers Apr 14 '14 at 16:01
  • yeah, i understand that the processes are different. I want to know if there's an API or something which can trigger flask app's methods from outside. a special route would solve the problem, but may be there's something internal.. I don't know. – KennyPowers Apr 14 '14 at 16:03
  • There is nothing pre-coded for you; a special route would be the easiest way to go about this. – Martijn Pieters Apr 14 '14 at 16:03
  • @MartijnPieters, thanks. I'm going to look at interprocess communication -- this is what I really want :) – KennyPowers Apr 14 '14 at 16:06

1 Answers1

0

I think app.app_context() is what you are looking for. But you need to make the context first like.

def _make_context():
    return dict(app=app, db=db, models=models)

manager = Manager(create_app)
manager.add_command("livereload", liveReloadServer, Shell(make_context=_make_context))

and then:

class liveReloadServer(Command):
    """prints hello world"""
    def run(self):
        with app.app_context():
            server = Server(app.wsgi_app)
            app.config['LIVERELOAD'] = True
            server.serve(port=8080)
CESCO
  • 7,305
  • 8
  • 51
  • 83