Skip to content
This repository

Point %pastebin to gist #1670

Merged
merged 2 commits into from almost 2 years ago

3 participants

Thomas Kluyver Matthias Bussonnier Fernando Perez
Thomas Kluyver
Collaborator

Unfortunately http://paste.pocoo.org/ has closed down. This updates the %pastebin magic to use gists instead. The results look like this: https://gist.github.com/2521969

We don't try to use any of the advanced features of gist, such as authenticating to make gists associated with your account. That could be added if there's demand for it, but for now we just send some content and return a URL.

Matthias Bussonnier
Collaborator

Change the docstring to github gist maybe.

Do you think a raw_input for an optionnal description could be usefull ?

Fernando Perez
Owner

Since gist allows for a file description, it would be nice to add that as an optional flag (the current default is fine if not given). Naming the file could also be done, but I think that's less important. But I think the description field could in fact be useful.

Thomas Kluyver
Collaborator

Thanks both, I've made those changes.

Thomas Kluyver
Collaborator

And tested them, of course: https://gist.github.com/2522172

Fernando Perez
Owner

Great! Merging now, thanks.

Fernando Perez fperez merged commit 970a0df into from April 28, 2012
Fernando Perez fperez closed this April 28, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 32 additions and 6 deletions. Show diff stats Hide diff stats

  1. 38  IPython/core/magic.py
38  IPython/core/magic.py
@@ -21,6 +21,7 @@
21 21
 import inspect
22 22
 import imp
23 23
 import io
  24
+import json
24 25
 import os
25 26
 import sys
26 27
 import shutil
@@ -30,7 +31,7 @@
30 31
 from StringIO import StringIO
31 32
 from getopt import getopt,GetoptError
32 33
 from pprint import pformat
33  
-from xmlrpclib import ServerProxy
  34
+from urllib2 import urlopen
34 35
 
35 36
 # cProfile was added in Python2.5
36 37
 try:
@@ -2231,15 +2232,40 @@ def magic_save(self,parameter_s = ''):
2231 2232
         print cmds
2232 2233
 
2233 2234
     def magic_pastebin(self, parameter_s = ''):
2234  
-        """Upload code to the 'Lodge it' paste bin, returning the URL."""
  2235
+        """Upload code to Github's Gist paste bin, returning the URL.
  2236
+        
  2237
+        Usage:\\
  2238
+          %pastebin [-d "Custom description"] 1-7
  2239
+          
  2240
+        The argument can be an input history range, a filename, or the name of a
  2241
+        string or macro.
  2242
+        
  2243
+        Options:
  2244
+        
  2245
+          -d: Pass a custom description for the gist. The default will say
  2246
+              "Pasted from IPython".
  2247
+        """
  2248
+        opts, args = self.parse_options(parameter_s, 'd:')
  2249
+        
2235 2250
         try:
2236  
-            code = self.shell.find_user_code(parameter_s)
  2251
+            code = self.shell.find_user_code(args)
2237 2252
         except (ValueError, TypeError) as e:
2238 2253
             print e.args[0]
2239 2254
             return
2240  
-        pbserver = ServerProxy('http://paste.pocoo.org/xmlrpc/')
2241  
-        id = pbserver.pastes.newPaste("python", code)
2242  
-        return "http://paste.pocoo.org/show/" + id
  2255
+        
  2256
+        post_data = json.dumps({
  2257
+          "description": opts.get('d', "Pasted from IPython"),
  2258
+          "public": True,
  2259
+          "files": {
  2260
+            "file1.py": {
  2261
+              "content": code
  2262
+            }
  2263
+          }
  2264
+        }).encode('utf-8')
  2265
+        
  2266
+        response = urlopen("https://api.github.com/gists", post_data)
  2267
+        response_data = json.loads(response.read().decode('utf-8'))
  2268
+        return response_data['html_url']
2243 2269
 
2244 2270
     def magic_loadpy(self, arg_s):
2245 2271
         """Load a .py python script into the GUI console.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.