Ticket #693 (needs_review enhancement)

Opened 2 years ago

Last modified 13 hours ago

Script to spawn a browser / start notebook.

Reported by: boothby Owned by: boothby
Priority: major Milestone: sage-4.3.4
Component: notebook Keywords:
Cc: was, mpatel, wjp, acleone Author(s): Tim Dumol, Mitesh Patel
Report Upstream: N/A Reviewer(s):
Merged in: Work issues:

Description

I've had an icon sitting on my desktop for about a week now. When I click on it, and it starts a notebook in a background terminal and spawns a browser. I'd like to be able to click it a second time, and open another browser window, instead of the current behavior of attempting to start another notebook.

Should work something like this:

if not notebook_is_running:
    notebook(settings from commandline, open_browser=True)
else:
    open_browser(settings from commandline)

Attachments

trac_693-spawn-browser-start-nb.patch Download (2.8 KB) - added by timdumol 4 months ago.
Modifies sage -notebook to launch a browser window if the notebook is already started. Also adds a sage -nb shortcut.
trac_693-spawn-browser-start-nb.2.patch Download (3.5 KB) - added by timdumol 4 months ago.
Checks if the notebook server is running too, instead of just checking if the PID exists.
trac_693-spawn-browser-start-nb.3.patch Download (3.5 KB) - added by timdumol 4 months ago.
Fixes bug with arguments.
trac_693-spawn-browser-start-nb.4.patch Download (3.1 KB) - added by timdumol 4 months ago.
Same thing, without actually checking if the Twistd process is running
trac_693-spawn-nb.patch Download (2.4 KB) - added by timdumol 4 months ago.
Changes run_notebook.py to launch a browser to the notebook page should an instance in the directory exist. Apply to sagenb repo. Apply this patch only.
trac_693-spawn-nb.2.patch Download (2.0 KB) - added by timdumol 3 months ago.
Uses signals only to check if the process exists (as Twisted does)
trac_693-spawn_notebook.3.patch Download (16.4 KB) - added by mpatel 8 weeks ago.
Open browser if server running and open_viewer=True. pep8 clean-ups. Rebased for queue in comment. Replaces previous.

Change History

Changed 4 months ago by timdumol

Modifies sage -notebook to launch a browser window if the notebook is already started. Also adds a sage -nb shortcut.

Changed 4 months ago by timdumol

  • cc was, mpatel added
  • status changed from new to needs_review
  • author set to Tim Dumol

This patch should do the trick. Apply to scripts repository.

Changed 4 months ago by timdumol

Checks if the notebook server is running too, instead of just checking if the PID exists.

Changed 4 months ago by timdumol

I am not sure which method is preferrable, since checking if the notebook server is running does not work if the user has no permission to read /proc and to send signals. Please feel free to approve of either patch.

Changed 4 months ago by timdumol

Fixes bug with arguments.

Changed 4 months ago by timdumol

Same thing, without actually checking if the Twistd process is running

Changed 4 months ago by was

  • status changed from needs_review to needs_work

I don't get it. If I do

$  sage -notebook directory=foo port=8001 & 
$  sage -notebook directory=bar port=8002 

then when I execute the second line it might just pop up a notebook server pointed at port 8001. Actually, given the line:

cmd = "notebook(" + ",".join([wrap(v) for v in sys.argv[1:]]) + ",port=" + SAGENB_PORT + ")"

I think it would give an error, since port= is specified twice.

This is because you introduced a new environment variable SAGENB_PORT which isn't documented. I don't know why it is there. I think you should get the port from the port= option on the command line.

I think you should get port= from the command line and get rid of the SAGENB_PORT environment variable.

Also, you use:

DOT_SAGENB = os.environ.get('DOT_SAGENB', os.path.join(os.environ['HOME'], '.sage', 'sage_notebook.sagenb'))

but actually, you need to use the file os.path.join(D, 'twistd.pid') where D is the option specified in directory= in the command line.

Finally, I think this code should be in the notebook(...) command in Sage itself. It's wrong putting it here in this shell script, because it only half way fixes the problem. E.g., imagine a user that types the following and leaves that in a console:

sage: notebook()

Then in another console, they type

sage: notebook()

Instead of giving an error, it should just given them the notebook. If you put this code that you've just written in the notebook command, then both problems are automatically solved, whereas with the current code location, only half the problem is really solved.

Also, there is a notebook(fork=True) option, so one can do

sage: notebook(fork=True)
The notebook files are stored in: sage_notebook.sagenb
**************************************************
*                                                *
* Open your web browser to http://localhost:8001 *
*                                                *
**************************************************
<pexpect.spawn instance at 0x10bb78bd8>
sage: notebook()
# get some notebook

William

Changed 4 months ago by timdumol

Changes run_notebook.py to launch a browser to the notebook page should an instance in the directory exist. Apply to sagenb repo. Apply this patch only.

Changed 4 months ago by timdumol

  • status changed from needs_work to needs_review

All your points make sense. I have implemented the changes in run_notebook.py. I've decided to check if the process is running, since that's also what twistd does.

Changed 3 months ago by was

  • status changed from needs_review to needs_work
  • upstream set to N/A

On OS X this doesn't work at all. Depending on what I do either I get two notebook servers running simultaneously on the same directory (bad), or I get "Another twistd server is running, PID 40940".

On OS X there is no /proc filesystem. However, when I run this code from this patch:

        try:
            print 1
            # First check using /proc directory
            if os.path.exists('/proc/%d'  % twistd_pid):
                launch_browser_to_nb()
                return
            else:
                remove_pidfile(twistd_pid_path)                
        except:
            print 2

I don't see "2" printed, i.e., no exception is raised. That's clear if you read the code -- you don't raise an exception.

Changed 3 months ago by timdumol

Uses signals only to check if the process exists (as Twisted does)

Changed 3 months ago by timdumol

  • status changed from needs_work to needs_review

I have removed the /proc check, since it's what Twisted does anyways.

Changed 2 months ago by mpatel

I guess we need to update the patch to take advantage of #2779?

Changed 8 weeks ago by mpatel

Open browser if server running and open_viewer=True. pep8 clean-ups. Rebased for queue in comment. Replaces previous.

Changed 8 weeks ago by mpatel

  • cc wjp added

V3:

  • If a server is running and open_viewer=True, get the settings from the old twistedconf.tac and browse to the server's home page.
  • Use return instead of sys.exit, in case of command-line invocation.
  • Some  pep8 changes.
  • Rebased for this queue
    sagenb-0.7 / #8051
    trac_7784-hgignore_update.2.patch
    trac_5712-interrupt-notification.6.patch
    trac_6069-missing_pub_ws.2.patch
    trac_8038-email_plus_addressing_v2.patch
    trac_7506-notebook_object-documentation.2.patch
    trac_693-spawn_notebook.3.patch
    

Changed 13 hours ago by timdumol

  • cc acleone added
  • reviewer set to Tim Dumol

Changed 13 hours ago by timdumol

  • reviewer Tim Dumol deleted
  • author changed from Tim Dumol to Tim Dumol, Mitesh Patel
Note: See TracTickets for help on using tickets.