Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes to the htmlnotebook #727

Merged
merged 3 commits into from Aug 25, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions IPython/frontend/html/notebook/handlers.py
Expand Up @@ -53,7 +53,7 @@ class NamedNotebookHandler(web.RequestHandler):
def get(self, notebook_id):
nbm = self.application.notebook_manager
if not nbm.notebook_exists(notebook_id):
raise web.HTTPError(404)
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
self.render('notebook.html', notebook_id=notebook_id)


Expand Down Expand Up @@ -311,7 +311,7 @@ class RSTHandler(web.RequestHandler):

def post(self):
if publish_string is None:
raise web.HTTPError(503)
raise web.HTTPError(503, u'docutils not available')
body = self.request.body.strip()
source = body
# template_path=os.path.join(os.path.dirname(__file__), u'templates', u'rst_template.html')
Expand All @@ -326,7 +326,7 @@ def post(self):
settings_overrides=defaults
)
except:
raise web.HTTPError(400)
raise web.HTTPError(400, u'Invalid RST')
print html
self.set_header('Content-Type', 'text/html')
self.finish(html)
Expand Down
27 changes: 14 additions & 13 deletions IPython/frontend/html/notebook/kernelmanager.py
Expand Up @@ -278,24 +278,20 @@ def start_kernel(self, notebook_id=None):

def kill_kernel(self, kernel_id):
"""Kill a kernel and remove its notebook association."""
if kernel_id not in self:
raise web.HTTPError(404)
self._check_kernel_id(kernel_id)
super(MappingKernelManager, self).kill_kernel(kernel_id)
self.delete_mapping_for_kernel(kernel_id)
self.log.info("Kernel killed: %s" % kernel_id)

def interrupt_kernel(self, kernel_id):
"""Interrupt a kernel."""
if kernel_id not in self:
raise web.HTTPError(404)
self._check_kernel_id(kernel_id)
super(MappingKernelManager, self).interrupt_kernel(kernel_id)
self.log.info("Kernel interrupted: %s" % kernel_id)

def restart_kernel(self, kernel_id):
"""Restart a kernel while keeping clients connected."""
if kernel_id not in self:
raise web.HTTPError(404)

self._check_kernel_id(kernel_id)
# Get the notebook_id to preserve the kernel/notebook association.
notebook_id = self.notebook_for_kernel(kernel_id)
# Create the new kernel first so we can move the clients over.
Expand All @@ -309,17 +305,22 @@ def restart_kernel(self, kernel_id):
return new_kernel_id

def create_iopub_stream(self, kernel_id):
if kernel_id not in self:
raise web.HTTPError(404)
"""Create a new iopub stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_iopub_stream(kernel_id)

def create_shell_stream(self, kernel_id):
if kernel_id not in self:
raise web.HTTPError(404)
"""Create a new shell stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_shell_stream(kernel_id)

def create_hb_stream(self, kernel_id):
if kernel_id not in self:
raise web.HTTPError(404)
"""Create a new hb stream."""
self._check_kernel_id(kernel_id)
return super(MappingKernelManager, self).create_hb_stream(kernel_id)

def _check_kernel_id(self, kernel_id):
"""Check a that a kernel_id exists and raise 404 if not."""
if kernel_id not in self:
raise web.HTTPError(404, u'Kernel does not exist: %s' % kernel_id)

29 changes: 14 additions & 15 deletions IPython/frontend/html/notebook/notebookmanager.py
Expand Up @@ -70,8 +70,7 @@ def list_notebooks(self):

def new_notebook_id(self, name):
"""Generate a new notebook_id for a name and store its mappings."""
notebook_id = unicode(uuid.uuid5(uuid.NAMESPACE_URL,
'file://'+self.get_path_by_name(name).encode('utf-8')))
notebook_id = unicode(uuid.uuid4())
self.mapping[notebook_id] = name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only suggestion would be to leave this commented out with a TODO indicating we want to get stable urls for which this mostly works, but which can't be left quite as-is because of the breakage. That way, if we do take a stab at proper url redirection, then we don't have to remember what this logic was. Because this part by itself is pretty much ok, the problem is the inconsistency generated on renames due to lack of redirection.

If you'd rather not leave dead code around, at least a todo comment with a note of this commit would make the job easier later if we need to get this back, so we don't need to go fishing for it in git. No big deal one way or another, though.

self.rev_mapping[name] = notebook_id
return notebook_id
Expand All @@ -94,7 +93,7 @@ def find_path(self, notebook_id):
try:
name = self.mapping[notebook_id]
except KeyError:
raise web.HTTPError(404)
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
return self.get_path_by_name(name)

def get_path_by_name(self, name):
Expand All @@ -107,7 +106,7 @@ def get_notebook(self, notebook_id, format=u'json'):
"""Get the representation of a notebook in format by notebook_id."""
format = unicode(format)
if format not in self.allowed_formats:
raise web.HTTPError(415)
raise web.HTTPError(415, u'Invalid notebook format: %s' % format)
last_modified, nb = self.get_notebook_object(notebook_id)
data = current.writes(nb, format)
name = nb.get('name','notebook')
Expand All @@ -117,7 +116,7 @@ def get_notebook_object(self, notebook_id):
"""Get the NotebookNode representation of a notebook by notebook_id."""
path = self.find_path(notebook_id)
if not os.path.isfile(path):
raise web.HTTPError(404)
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
info = os.stat(path)
last_modified = datetime.datetime.utcfromtimestamp(info.st_mtime)
with open(path,'r') as f:
Expand All @@ -126,7 +125,7 @@ def get_notebook_object(self, notebook_id):
# v1 and v2 and json in the .ipynb files.
nb = current.reads(s, u'json')
except:
raise web.HTTPError(404)
raise web.HTTPError(500, u'Unreadable JSON notebook.')
if 'name' not in nb:
nb.name = os.path.split(path)[-1].split(u'.')[0]
return last_modified, nb
Expand All @@ -138,18 +137,18 @@ def save_new_notebook(self, data, name=None, format=u'json'):
and the value in the data is updated to use that value.
"""
if format not in self.allowed_formats:
raise web.HTTPError(415)
raise web.HTTPError(415, u'Invalid notebook format: %s' % format)

try:
nb = current.reads(data, format)
except:
raise web.HTTPError(400)
raise web.HTTPError(400, u'Invalid JSON data')

if name is None:
try:
name = nb.metadata.name
except AttributeError:
raise web.HTTPError(400)
raise web.HTTPError(400, u'Missing notebook name')
nb.metadata.name = name

notebook_id = self.new_notebook_id(name)
Expand All @@ -159,12 +158,12 @@ def save_new_notebook(self, data, name=None, format=u'json'):
def save_notebook(self, notebook_id, data, name=None, format=u'json'):
"""Save an existing notebook by notebook_id."""
if format not in self.allowed_formats:
raise web.HTTPError(415)
raise web.HTTPError(415, u'Invalid notebook format: %s' % format)

try:
nb = current.reads(data, format)
except:
raise web.HTTPError(400)
raise web.HTTPError(400, u'Invalid JSON data')

if name is not None:
nb.metadata.name = name
Expand All @@ -173,18 +172,18 @@ def save_notebook(self, notebook_id, data, name=None, format=u'json'):
def save_notebook_object(self, notebook_id, nb):
"""Save an existing notebook object by notebook_id."""
if notebook_id not in self.mapping:
raise web.HTTPError(404)
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
old_name = self.mapping[notebook_id]
try:
new_name = nb.metadata.name
except AttributeError:
raise web.HTTPError(400)
raise web.HTTPError(400, u'Missing notebook name')
path = self.get_path_by_name(new_name)
try:
with open(path,'w') as f:
current.write(nb, f, u'json')
except:
raise web.HTTPError(400)
raise web.HTTPError(400, u'Unexpected error while saving notebook')
if old_name != new_name:
old_path = self.get_path_by_name(old_name)
if os.path.isfile(old_path):
Expand All @@ -196,7 +195,7 @@ def delete_notebook(self, notebook_id):
"""Delete notebook by notebook_id."""
path = self.find_path(notebook_id)
if not os.path.isfile(path):
raise web.HTTPError(404)
raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_id)
os.unlink(path)
self.delete_notebook_id(notebook_id)

Expand Down
9 changes: 9 additions & 0 deletions IPython/frontend/html/notebook/static/js/codecell.js
Expand Up @@ -190,6 +190,15 @@ var IPython = (function (IPython) {
};


CodeCell.prototype.select_all = function () {
var start = {line: 0, ch: 0};
var nlines = this.code_mirror.lineCount();
var last_line = this.code_mirror.getLine(nlines-1);
var end = {line: nlines-1, ch: last_line.length};
this.code_mirror.setSelection(start, end);
};


CodeCell.prototype.append_output = function (json) {
this.expand();
if (json.output_type === 'pyout') {
Expand Down
2 changes: 1 addition & 1 deletion IPython/frontend/html/notebook/static/js/notebook.js
Expand Up @@ -776,7 +776,7 @@ var IPython = (function (IPython) {
cell.render();
}
if (default_options.terminal) {
cell.clear_input();
cell.select_all();
} else {
if ((cell_index === (that.ncells()-1)) && default_options.add_new) {
that.insert_code_cell_below();
Expand Down
27 changes: 13 additions & 14 deletions docs/source/interactive/htmlnotebook.txt
Expand Up @@ -6,7 +6,7 @@ An HTML Notebook IPython

The IPython Notebook consists of two related components:

* An XML/JSON based Notebook document format for recording and distributing
* An JSON based Notebook document format for recording and distributing
Python code and rich text.
* A web-based user interface for authoring and running notebook documents.

Expand All @@ -24,7 +24,7 @@ which will behave similar to the terminal and Qt console versions, using your
default matplotlib backend and providing floating interactive plot windows. If
you want inline figures, you must manually select the ``inline`` backend::

$ ipython notebook --pylab inline
$ ipython notebook --pylab=inline
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was nothing wrong here, the '=' sign is unnecessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but are we recommending either as the canonical way of passing arguments?

On Wed, Aug 24, 2011 at 10:25 AM, minrk
reply@reply.github.com
wrote:

@@ -24,7 +24,7 @@ which will behave similar to the terminal and Qt console versions, using your
 default matplotlib backend and providing floating interactive plot windows.  If
 you want inline figures, you must manually select the inline backend::

  •    $ ipython notebook --pylab inline
  •    $ ipython notebook --pylab=inline

There was nothing wrong here, the '=' sign is unnecessary.

Reply to this email directly or view it on GitHub:
https://github.com/ipython/ipython/pull/727/files#r101556

Brian E. Granger
Cal Poly State University, San Luis Obispo
bgranger@calpoly.edu and ellisonbg@gmail.com


This server uses the same ZeroMQ-based two process kernel architecture as
the QT Console as well Tornado for serving HTTP requests. Some of the main
Expand All @@ -33,7 +33,7 @@ features of the Notebook include:
* Display rich data (png/html/latex/svg) in the browser as a result of
computations.
* Compose text cells using HTML and Markdown.
* Import and export notebook documents in range of formats (.ipynb, .json, .py).
* Import and export notebook documents in range of formats (.ipynb, .py).
* In browser syntax highlighting, tab completion and autoindentation.
* Inline matplotlib plots that can be stored in Notebook documents and opened
later.
Expand All @@ -47,7 +47,7 @@ how to install the notebook and its dependencies.
work on notebooks in different directories. By default the first notebook
server starts in port 8888, later notebooks search for random ports near
that one. You can also manually specify the port with the ``--port``
option, if you want persistent URLs you can bookmark.
option.


Basic Usage
Expand All @@ -59,14 +59,14 @@ in which the application was started, and allows you to create new notebooks.

A notebook is a combination of two things:

1. an interactive session connected to an IPython kernel, controlled by a web
1. An interactive session connected to an IPython kernel, controlled by a web
application that can send input to the console and display many types of output
(text, graphics, mathematics and more). This is the same kernel used by the
:ref:`Qt console <qtconsole>`, but in this case the web console sends input in
persistent cells that you can edit in-place instead of the vertically scrolling
terminal style used by the Qt console.

2. a document that can save the inputs and outputs of the session as well as
2. A document that can save the inputs and outputs of the session as well as
additional text that accompanies the code but is not meant for execution. In
this way, notebook files serve as a complete computational record of a session
including explanatory text and mathematics, code and resulting figures. These
Expand All @@ -86,11 +86,10 @@ Creating and editing notebooks
You can create new notebooks from the dashboard with the ``New Notebook``
button or open existing ones by clicking on their name. Once in a notebook,
your browser tab will reflect the name of that notebook (prefixed with "IPy:").
The URL for that notebook is not meant to be human-readable, but it is
persistent across invocations of the notebook server *as long as you don't
rename the notebook*, so you can bookmark them for future use.
The URL for that notebook is not meant to be human-readable and is *not*
persistent across invocations of the notebook server.

You can also drag and dropp into the area listing files any python file: it
You can also drag and drop into the area listing files any python file: it
will be imported into a notebook with the same name (but ``.ipynb`` extension)
located in the directory where the notebook server was started. This notebook
will consist of a single cell with all the code in the file, which you can
Expand Down Expand Up @@ -133,7 +132,7 @@ Text input

In addition to code cells and the output they procude (such as figures), you
can also type text not meant for execution. To type text, change the type of a
cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`C-m m`
cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m`
keybinding (see below). You can then type any text in Markdown_ syntax, as
well as mathematical expressions if you use ``$...$`` for inline math or
``$$...$$`` for displayed math.
Expand Down Expand Up @@ -167,7 +166,7 @@ in comment areas.
through the Python form. You should think of the Python format as a way to
output a script version of a notebook and the import capabilities as a way
to load existing code to get a notebook started. But the Python version is
*not* an alternate Python format.
*not* an alternate notebook format.


Keyboard use
Expand All @@ -185,13 +184,13 @@ key bindings you need to remember are:
:kbd:`Shift-Enter` to get execution (or use the mouse and click on the ``Run
Selected`` button).

* :kbd:`Control-Enter`: execute the current cell in "terminal mode", where any
* :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any
output is shown but the cursor cursor stays in the current cell, whose input
area is flushed empty. This is convenient to do quick in-place experiments
or query things like filesystem content without creating additional cells you
may not want saved in your notebook.

* :kbd:`Control-m`: this is the prefix for all other keybindings, which consist
* :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist
of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole
letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining
available keybindings.
Expand Down