Ticket #693: trac_693-spawn-browser-start-nb.3.patch

File trac_693-spawn-browser-start-nb.3.patch, 3.5 KB (added by timdumol, 12 years ago)

Fixes bug with arguments.

  • sage-notebook

    #693 Script to spawn a browser/start notebook.
    
    diff -r d0d2608c899a sage-notebook
    a b  
    22
    33import os, sys, socket
    44
    5 print open(os.environ['SAGE_ROOT'] + '/local/bin/sage-banner').read()
    6 
    7 print "Please wait while the Sage Notebook server starts..."
    8 
    9 from sage.server.notebook.all import notebook
    10 
    11 print "..."
    12 
    135def wrap(s):
    146    """
    157    INPUT:
    16         s -- string
     8    s -- string
    179    OUTPUT:
    18         string
     10    string
    1911
    2012    Do nothing to True, False or positive integer.  Wrap everything
    2113    else in a raw triple quoted string.
     
    2820    if s.isdigit(): return s
    2921    return "r'''%s'''"%s
    3022
    31 if len(sys.argv) > 1:
    32     cmd = "notebook(" + ",".join([wrap(v) for v in sys.argv[1:]]) + ")"   
    33     print cmd
    34     exec cmd
    35 else:
    36     notebook(port=8000)
     23def launch_browser_to_nb():
     24    from sage.misc.viewer import viewer, browser
     25    viewer()
     26    os.system(browser() + ' http://localhost:%s' % SAGENB_PORT)
     27    exit(0)
     28if __name__ == '__main__':
     29    DOT_SAGENB = os.environ.get('DOT_SAGENB', os.path.join(os.environ['HOME'], '.sage', 'sage_notebook.sagenb'))
     30    SAGENB_PORT = os.environ.get('SAGENB_PORT', '8000')
     31    twistd_pid_path = os.path.join(DOT_SAGENB, 'twistd.pid')
     32    if os.path.exists(twistd_pid_path):
     33        twistd_pid = open(twistd_pid_path).read()
     34        # Detect if the process is running
     35        try:
     36            # First see if there is a file in /proc
     37            if os.exists('/proc/%s' % twistd_pid):
     38                launch_browser_to_nb()
     39        except:
     40            # No permission to access /proc, try sending a signal
     41            try:
     42                os.kill(twistd_pid, 0)
     43                launch_browser_to_nb()
     44            except:
     45                pass
     46        os.unlink(twistd_pid_path)
     47
     48    print open(os.environ['SAGE_ROOT'] + '/local/bin/sage-banner').read()
     49    print "Please wait while the Sage Notebook server starts..."
     50    from sage.server.notebook.all import notebook
     51    print "..."
     52    if len(sys.argv) > 1:
     53        cmd = "notebook(" + ",".join([wrap(v) for v in sys.argv[1:]]) + ",port=" + SAGENB_PORT + ")"   
     54        print cmd
     55        exec cmd
     56    else:
     57        notebook(port=int(SAGENB_PORT))
     58
  • sage-sage

    diff -r d0d2608c899a sage-sage
    a b  
    88    echo "  -coverage <files>  -- give info about doctest coverage of files"
    99    echo "  -h            -- print this help message"
    1010    echo "  -v, -version  -- print the Sage version"
    11     echo "  -notebook [options] -- start the Sage notebook (options are"
    12     echo "                   the same as to the notebook command in Sage)"
     11    echo "  -notebook [options], -nb [options] -- start the Sage notebook (options are"
     12    echo "                   the same as to the notebook command in Sage)."
     13    echo "                   Opens a browser window with the notebook if the notebook has already started."
    1314    echo "  -inotebook [options] -- start the *insecure* Sage notebook "
    1415    echo "  -i [packages] -- install the given Sage packages"
    1516    echo "  -min ...      -- do not populate global namespace (must be first option)"
     
    510511# Run Sage's versions of the standard Algebra/Geometry etc. software
    511512#####################################################################
    512513
    513 if [ $1 = "-notebook"  -o $1 = '--notebook' ]; then
     514if [ $1 = "-notebook"  -o $1 = '--notebook' -o $1 = '-nb' ]; then
    514515   cd "$CUR"
    515516   shift
    516517   sage-cleaner &