Tweet me... said an IPython notebook cell and I did it

  |   Source   |   Minimap

It is time to release some extensions I wrote the last weeks. And because it is Friday, and it is a great moment to begin with the "relax" and procrastination, I will release the tweet me extension...

You don't have any idea what I am talking about? OK, a few words... Probably you know that spend a lot of time inside IPython notebooks and I was tired to switch windows to send or answer a tweet. So, I wrote a little js extension to capture the input of the selected cell and tweet it just pressing the twitter icon at the IPython toolbar.

But this js extension is IPython-powered itself, because I am using the IPython js machinery to execute python files calling the python kernel! Yes... it seems weird, but once you get the idea, you can achieve a lot of things. You can see the relevant part of the code below:

In [1]:
!sed -n 59,74p /media/datos/Desarrollos/mIPyex/custom/tweet_me/main.js
  Click me to hide the output
function tweetMe(path) {
  var entry = IPython.notebook.get_selected_cell().get_text();
  if (entry.length == 0) {
    zeroMessager(); 
  }
  if (entry.length > 0 && entry.length <= 140) {
    IPython.notebook.kernel.execute('%bookmark root');
    IPython.notebook.kernel.execute('%cd ' + path);
    IPython.notebook.kernel.execute('%run tweet_helper.py "' + entry + '"');
    IPython.notebook.kernel.execute('%cd -b root');
    sucessMessager();
  }
  if (entry.length > 140) {
    wrongMessager();
  }
}

The secret is to use the IPython.notebook.kernel.execute(whatever) method and pass it whatever you want, even IPython magics functions! Yes again... too much Inception movie for the last weeks ;-)

OK, the extension lives in this new repo: mIPyex, where I will upload the development versions of my own extensions. When I get them enough stable, I will make a copy of them in the IPython notebook extensions repo where you can get a lot of and useful interesting extensions.

Do you want to try it? It is easy... just follow the README to install all the necessary tools (I used the PTT library to deal with the Twitter API) and then just click the twitter button when you want to share something with the world!

Hope you enjoy it a lot!

Damián

PD: I will use the next cell to tweet about this new blog post! ;-)

New blog post! Tweet me... said an IPython notebook cell and I did it. http://goo.gl/ry9Umi

Addendum: You can now send tweets from cells with multi-line content (my first implementation only worked with one-line strings).

Addendum 2: You have not only a button but also a keyboard shortcut to activate the Tweet me extension: ALT + T.

Did you like the content? Great!

Or visit my support page for more information.


Btw, don't forget this blog post is an ipynb file itself! So, you can download it from the "Source" link at the top of the post if you want to play with it ;-)

Comments powered by Disqus