Hydrogen: Idea: Create ipynb notebook file from Hydrogen

Created on 22 May 2017  路  24Comments  路  Source: nteract/hydrogen

It would be cool to be able to export notebooks from a plain text file use by Hydrogen.

Hydrogen has support for multiple cells. It would be great to be able to play nicely with nteract and jupyter notebook.

Where to start?

  • The current text editor in Hydrogen can be accessed via store.editor
  • getCells returns the text editor ranges of all cells
  • getTextInRange returns the text content of a given range
  • @nteract/commutable can create a notebook data structure which then can be saved to disk
enhancement hacktoberfest mozsprint

Most helpful comment

I'll check this out. Been meaning to look into hydrogen for sometime.

All 24 comments

Another cool thing we have is that we can automatically build a notebook in the background too, using all the execute_input messages for source and all the outputs. Getting the current view as a notebook would be cool too.

I'll check this out. Been meaning to look into hydrogen for sometime.

For anyone who needs this right now, I have uploaded my interim solution as a gist here.

@lgeiger So, unless you begin a block of code with a comment that has %% in it, all the contents of the editor are taken to be just one cell ? For the Run All command.

Maybe I didn't look hard enough, but that wasn't too clear from the docs. I know the example gif in the README showed the %% in the comments, but my eyes usually glide over comments most of the time :)

(Mental reminder to make that obvious from the docs)

milestone suggestion:

  1. a low-hanging fruit is to just save the entire atom pane with the input and output as an HTML file, so people can immediately export to share with people as-is
  2. the next stage is to dig deeper into this HTML div and extract the elements

related thing done for saving a preview pane of a markdown file as-is into an HTML file: https://github.com/atom/markdown-preview/pull/68

See the screenshot for the targetable HTML div.

screen shot 2017-10-08 at 12 47 15 pm

Thanks @kengz , I think I've got this.

@kengz, @Madhu94 Thanks a lot for your interest in implementing this feature.

Rather than using the HTML on the page which will become messy very fast, the easiest solution is to use @nteract/commutable as roughly outlined above.
Commutable is also used inside nteract to do all notebook manipulation and will have all you need.

Feel free to join our Slack if you have more questions.

@lgeiger I am doing it with @nteract/commutable as you suggested in the first update. I have not worked out how to get the outputs exactly (I think they're in store.kernel.OutputStore). Once I get a minimal version working, I'll put up a WIP pull request.

@lgeiger Thanks for leaving this feature for new contributors. I absolutely love the Hydrogen codebase.

Great that you're working on it.
For the first working version I'd focus on the source code and wouldn't worry too much about the output, we can always do this in future PR's. Looking forward to it.

It's also possible to grab Hydrogen output via Stitch or Pandoctools. The latter can export to ipynb and designed to leverage the full power of Pandoc filters (Jupyter output collecting is also a Pandoc filter).

See this usage code example on Stackoverflow.

@lgeiger , I see that there has been a PR on this, but how can I use this with Hydrogen in Atom? I cannot find documentation on how to do this.

There will be more documentation once https://github.com/nteract/hydrogen/pull/1498 gets merged

@kylebarron, thanks. I got confused because here it shows it as merged and the branch deleted more than a year ago. So I thought this is part of the package now.

It is, it's the "export notebook" command, it just isn't documented well

Do you know how I can call it, or have any example? Is it like run-cell and run-all commands or sth different? I can certainly wait though for #1498 to get merged.

You just call "export notebook", and you just need to have cell markers. See the "run cell" docs

@kylebarron, thanks. It worked really well. Do you know if option 1 here (converting the hydrogen output to html) has been implemented or not?

It hasn't been implemented and I agree with @lgeiger that it's not a good use of time, because if we have good export to a notebook, then you can use nbconvert to convert to HTML

@kylebarron and @lgeiger, I agree to some extent. However, nbconvert is an additional step which also needs to run the code in order to generate the output. This would be waste of time if the code takes time to run. Once the output is generated from atom though, I think there should be option to take a "snapshot" and save it as html. Like in Spyder, one can just right click on the ipython terminal and save the whole output as html. I addition, adding such options makes users independent of any extra tools other than Hydrogen+atom.

No, nbconvert doesn't run the code, it just exports whatever is already saved in the notebook file.

Cool. So this means that one can run the code in Hydrogen, then save the code with the outputs using #1498 to a notebook, and then use nbconvert to get html, right? I am interested in having the outputs (and possibly the code) saved to html file.

At present it only exports code and not results. See #1503

Nice! I'll wait then. Thanks for all the info.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasonleonhard picture jasonleonhard  路  3Comments

HaoranXue picture HaoranXue  路  4Comments

ethan92429 picture ethan92429  路  3Comments

mda6 picture mda6  路  3Comments

onyxfish picture onyxfish  路  4Comments