If you work with Python regularly, you probably know about IPython already. IPython has web based notebooks, QT based GUI consoles and plain old simple Terminal based REPL which is simply fantastic. But that’s not all, we can also embed IPython in our applications too. And this can lead to a number of potential use cases.
Use Cases
A common use case could be to drop into a IPython shell for quick interactive debugging. This can come very handy during prototyping.
Let’s see an example:
1 2 3 4 5 |
import IPython name = "Masnun" IPython.embed() |
When we run this code, we will get a nice IPython REPL where we can try out things. In our case, we haven’t done much except defining a variable named name
. We can print it out.
1 2 |
In [1]: print(name) Masnun |
I use Iron.io workers/queues/caches at my day to day job. So I often need to check status of the workers or get the size of a queue or even queue a few workers. I also need to check a few records on Mongodb. An interactive prompt can be really helpful for these.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import IPython from iron_worker import IronWorker from iron_mq import IronMQ from iron_worker import Task from pymongo import MongoClient worker = IronWorker(project_id="PROJECT_ID", token="TOKEN") mq = IronMQ(project_id="PROJECT_ID", token="TOKEN") mongo = MongoClient("MONGOURL") def launch_workers(name, number=1): for x in range(number): task = Task(code_name=name) worker.queue(task) print("Launched {} workers for {}".format(name, number)) def top_buyers(): customers_collection = mongo.shop.customers query = { "purchases": {"$gte": 100} } print(customers_collection.find(query).count()) IPython.embed() |
Now I can just do launch_workers("send_emails", 3)
to launch 3 worker instances for the “send_emails” worker. Or get the number of buyers with more than 100 purhcases with the top_buyers()
function.
Customizing The Prompt
When we embed IPython, it displays it’s common banner when starting.
1 2 3 4 5 6 7 8 |
Python 2.7.8 (default, Nov 15 2014, 03:09:43) Type "copyright", "credits" or "license" for more information. IPython 2.3.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. |
We can easily disable that. To do so, we need to pass empty string to the banner1
parameter to the embed
method.
1 |
IPython.embed(banner1="") |
Or we can further customize the 2nd banner or the exit message like this:
1 2 3 4 5 |
IPython.embed( banner1="Entering Debug Mode", banner2="Here's another helpful message", exit_msg="Exiting the debug mode!" ) |
2 replies on “Embedding IPython in your application”
Here you’ve stopped application and switched to ipython console.
Much more intresting will be method not to stop, but connect to application with telnet/ssh, or maybe with ipython itself, it has some interprocess communication builtin.
This seem like an interesting idea. I would love to explore further.