Issue
I'm writing a bunch of examples of how to use Flask
and currently I want to be able to just write
@app.route("/")
def redirectora():
redirect("redirected")
@app.route("/targeta", methods=["GET", "POST"])
def redirecteda():
return "Redirected!"
app.run(host="0.0.0.0", port=5000)
in my cells but I have to include
app = Flask("the_flask_module")
in every cell (otherwise if I run the cell twice I get an assertion error about trying to add a route twice).
I would really like to be able to run code before the cell is run (so I don't have to write app =
60 times) but I'm not finding any signals or hooks for notebooks.
Does anyone know how run code before any cell is run?
Solution
The answer given by shad is the right one for the scenario outlined but the wrong one for the question as phrased (and what I was searching for when landing here). For anyone else who is looking for the right way to run code before a cell runs in ipython, check out the events api.
For example how the autoreload extension works
def load_ipython_extension(ip):
"""Load the extension in IPython."""
auto_reload = AutoreloadMagics(ip)
ip.register_magics(auto_reload)
ip.events.register("pre_run_cell", auto_reload.pre_run_cell)
ip.events.register("post_execute", auto_reload.post_execute_hook)
Answered By - George Mauer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.