Richard Jones' Log: The dynamic Cheese Shop: JSON package information

Sat, 24 Jul 2010

I don't get to work on the Cheese Shop very often these days. I've been wanting to add this feature for a while though: being able to dynamically include the latest information about a package on some other website. Now I can!

On the Roundup project website I have a "download" box. People have asked that I include the version information in that box, but I'm lazy and don't want to have to update it manually when I do a release. Now the page includes this HTML:

<span id="release_info" class="note">Download:
  <a href="http://pypi.python.org/pypi/roundup">latest</a></span>
<script type="text/javascript">
 $.getJSON('http://pypi.python.org/pypi/roundup/json?callback=?', function(data) {
     h = 'Download: ' + data.info.version;
     for (var i=0, url; url=data.urls[i]; ++i) {
       h += '<br><a href="' + url.url + '">' + url.filename + '</a>';
     }
     $('#release_info').html(h);
 });
</script>

This was actually generated in Sphinx, and the magic there is:

.. raw:: html

   <span id="release_info" class="note">Download: 
     <a href="http://pypi.python.org/pypi/roundup">latest</a></span>
   <script type="text/javascript">
    $.getJSON('http://pypi.python.org/pypi/roundup/json?callback=?', function(data) {
     h = 'Download: ' + data.info.version;
     for (var i=0, url; url=data.urls[i]; ++i) {
       h += '<br><a href="' + url.url + '">' + url.filename + '</a>';
     }
     $('#release_info').html(h);
    });
   </script>

That code uses jQuery which is already included in a Sphinx build so there's nothing more to do.

On the Cheese Shop side you have the option to request the JSON for the latest release or a specific release. The following are (currently, until I do a new Roundup release) equivalent:

http://pypi.python.org/pypi/roundup/1.4.15/json
http://pypi.python.org/pypi/roundup/json

Note in the jQuery the callback guff - that's part of some JSONP thing I don't care to understand but took me the longest of all of the implementation to work around. Boo for very average documentation, guys...