1

By providing only a status ID or a URL, I simply want a tweet status nicely to be rendered (i.e. in Twitter cards format) in my iPython Notebook. This can easily be achieved in WordPress simply by putting the link into the post.

First, tried the embed code provided by Twitter, which appears when 'more'>'embed tweet' button on the status page clicked. Successfully inserted into the NB but the embed code contains the tweet text (I don't want to go to twitter status page and click buttons for every tweet I want to display, instead looking for a way to automate this process):

HTML('<blockquote class="twitter-tweet" lang="en"><p>Sen. Joni Ernst of Iowa is set to give the Republican response to the President&#39;s <a href="https://twitter.com/hashtag/CNNSOTU?src=hash">#CNNSOTU</a>. <a href="http://t.co/PXWVbNsWXp">http://t.co/PXWVbNsWXp</a></p>&mdash; CNN Breaking News (@cnnbrk) <a href="https://twitter.com/cnnbrk/status/557737268488974338">January 21, 2015</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>')

Second, tried to import with iFrame, but this returned an empty iframe:

HTML('<iframe src=https://twitter.com/cnn/status/557737452346294274 width=700 height=350></iframe>')

with this error message (note: I also tried appending &output=embed to the url as suggested) :

Refused to display 'https://twitter.com/cnn/status/557737452346294274' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

Third, tried including Twitter's widgets.js script to NB as suggested :

from IPython.display import Javascript
Javascript('window.twttr = (function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0], t = window.twttr || {}; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); t._e = []; t.ready = function(f) { t._e.push(f);  };   return t; }(document, "script", "twitter-wjs"));')

And expected to see by calling (this is probably where I'm doing wrong) :

HTML('<iframe src=https://api.twitter.com/1.1/statuses/oembed.json?id=557737268488974338></iframe>')

But got this error instead :

{"errors":[{"code":215,"message":"Bad Authentication data."}]}

This should be something very simple. That is probably why it has not already been asked on SO. I am not experienced in JS, so please take this into account in your answers. Thanks.

Community
  • 1
  • 1
tozCSS
  • 5,487
  • 2
  • 34
  • 31

1 Answers1

0

You could download it first via some means (shown below is a %download magic I use) and then use the iframe.

IFrame with local html page

Doug Blank
  • 2,031
  • 18
  • 36
  • Thanks, and saw [your comment](https://github.com/ipython/ipython/issues/5415) on github, so does this magic command only works on Jupyter version, the one that I used on MS Windows (came with WinPython3.x) failed to find it? – tozCSS Mar 27 '15 at 21:19
  • The %download magic is a metakernel magic, not an IPython magic. But as the github issue suggests, one could make one for IPython pretty easily. – Doug Blank Mar 28 '15 at 22:35
  • How do we install install `MetaKernel`? – alvas Jul 07 '16 at 02:53