Hydrogen: Support carriage return symbol

Created on 4 Oct 2016  路  11Comments  路  Source: nteract/hydrogen

Hydrogen doesn't respect \r symbol

print('ab\rcd') will produce cd in console and/or jupyter notebook. In hydrogen it will show up as

cd

This is especially critical when working on scripts that report progress, e.g. use tqdm

Atom version: 1.10.2
Hydrogen version: 0.14.0

enhancement

All 11 comments

cc/ @rgbkrk

I believe this is a bug in the latest version of Jupyter. Both Hydrogen and IPython 2.4.1 produce the expected output:

cd

I add the bug label again since the output looks like this:
bildschirmfoto 2016-10-12 um 11 23 28
It should be a multiline bubble otherwise it breaks the layout of the following code line.

ipython 2.4.1 was released almost a year ago. Current version is 5.1.0 which produces cd in output.
selection_053

This is used mostly in progressbars (e.g. keras model training).
In hydrogen output produced can span several hunreds lines.

@kilotaras Oh, I see. Now, I understand what the issue is. I'm going to label this issue as a feature request.

@lgeiger I reckon this is best implemented in transformime.

Yes we should definitely handle this in transformime.

I still don't fully understand the expected behavior.
Is the \r supposed to remove the two surrounding characters? How would that help with progress bars?

r (by itself with no newline) is a carriage return. It returns the console writer to the first position.

Historically, this "carriage" was the printer head on a type writer. When you hit the key, the printer head would go back to the start - allowing you to type over the line you were just on.

carriage return

Using this, you can overwrite the previous line.

The more general problem is to handle more of the character codes to allow the cursor to move all over. You basically need to manage a TTY then, which, if you're interested I'm happy to explore.

Ahh thanks. I misread the expected behavior.
I read ad instead of cd. That makes sense now!

Thejupyter notebook handles it like this: https://github.com/jupyter/notebook/blob/master/notebook/static/base/js/utils.js#L446

I tried:

print('this sentence\rthat\nwill make you pause')

jupyter notebook prints:

will make you pause

jupyter console prints:

that sentence
will make you pause

As far as progress bars go, #474 does not resolve this:
image

It seems that since output is updated over time, \rs get ignored.

@lgeiger - you may want to check out stdout and stderr joining as is done in nteract.

However, I'd probably combine stdout and stderr to be more like a terminal.

function reduceOutputs(outputs, output) {
  const lastOutputIndex = outputs.length - 1
  if (outputs[lastOutputIndex].output_type === 'stream' && output.output_type === 'stream') {
    outputs[lastOutputIndex].text = outputs[lastOutputIndex].text + output.text;
    return outputs
  }
  outputs.push(output);
  return outputs;
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

homocomputeris picture homocomputeris  路  4Comments

wadethestealth picture wadethestealth  路  3Comments

Mungoid picture Mungoid  路  4Comments

onyxfish picture onyxfish  路  4Comments

lgeiger picture lgeiger  路  3Comments