Package sage :: Package server :: Package simple :: Module twist
[hide private]
[frames] | no frames]

Module twist

source code


This module provides a very simple API for interacting with a Sage session
over http. It runs in as part of the notebook server. 

NOTE: 
    The exact data in the JSON header may vary over time (for example, further
    data may be added), but should remain backwards compatable if it is being 
    parsed as JSON data. 


TESTS:

Start the notebook.  
    sage: from sage.server.misc import find_next_available_port
    sage: port = find_next_available_port(9000, verbose=False)
    sage: from sage.server.notebook.notebook_object import test_notebook
    sage: passwd = str(randint(1,1<<128))
    sage: nb = test_notebook(passwd, secure=False, address='localhost', port=port, verbose=True) #doctest: +ELLIPSIS 
    ...
    Notebook started.

Import urllib:
    sage: import urllib, re
    sage: def get_url(url): h = urllib.urlopen(url); data = h.read(); h.close(); return data

    
Login to a new session:
    sage: sleep(1)
    sage: login_page = get_url('http://localhost:%s/simple/login?username=admin&password=%s' % (port, passwd))
    sage: print login_page # random session id
    {
    "session": "2afee978c09b3d666c88b9b845c69608"
    }
    ___S_A_G_E___
    sage: session = re.match(r'.*"session": "([^"]*)"', login_page, re.DOTALL).groups()[0]

Run a command:
    sage: sleep(0.5)
    sage: print get_url('http://localhost:%s/simple/compute?session=%s&code=2*2' % (port, session))
    {
    "status": "done",
    "files": [],
    "cell_id": 1
    }
    ___S_A_G_E___
    4

Do a longer-running example: 
    sage: n = next_prime(10^25)*next_prime(10^30)
    sage: print get_url('http://localhost:%s/simple/compute?session=%s&code=factor(%s)&timeout=0.1' % (port, session, n))
    {
    "status": "computing",
    "files": [],
    "cell_id": 2
    }
    ___S_A_G_E___

Get the status of the computation:
    sage: print get_url('http://localhost:%s/simple/status?session=%s&cell=2' % (port, session))
    {
    "status": "computing",
    "files": [],
    "cell_id": 2
    }
    ___S_A_G_E___

Interrupt the computation: 
    sage: _ = get_url('http://localhost:%s/simple/interrupt?session=%s' % (port, session))
    
Test out getting files:
    sage: code = "h = open('a.txt', 'w'); h.write('test'); h.close()"
    sage: print get_url('http://localhost:%s/simple/compute?session=%s&code=%s' % (port, session, urllib.quote(code)))
    {
    "status": "done",
    "files": ["a.txt"],
    "cell_id": 3
    }
    ___S_A_G_E___

    sage: print get_url('http://localhost:%s/simple/file?session=%s&cell=3&file=a.txt' % (port, session))
    test
    
Log out:
    sage: _ = get_url('http://localhost:%s/simple/logout?session=%s' % (port, session))
    sage: nb.dispose()



Classes [hide private]
  SessionObject
  LoginResource
  LogoutResource
  InterruptResource
  RestartResource
  CellResource
  ComputeResource
  StatusResource
  FileResource
This differs from the rest as it does not print a header, just the raw file data.
  SimpleServer
Functions [hide private]
 
late_import() source code
 
simple_jsonize(data)
This may be replaced by a JSON spkg."...
source code
Variables [hide private]
  sessions = {}
  late_import_done = False
Function Details [hide private]

simple_jsonize(data)

source code 

This may be replaced by a JSON spkg."

EXAMPLES: 
    sage: from sage.server.simple.twist import simple_jsonize
    sage: print simple_jsonize({'a': [1,2,3], 'b': "yep"})
    { "a": [1, 2, 3], "b": "yep" }