Search code examples
githubjupyter-notebookcell

GitHub, Jupyter Notebook cells are truncated


Unfortunately I can not find a solution despite intensive search.

I want to push a Jupyter Notebook to GitHub. When I then view the Notebook on GitHub, cells with long code are truncated and the content cannot be reused via copy & paste.

Is there any possibility to use scrollbars in cells in GitHub or maybe include a pre-commit hook for automated formatting of line breaks? Or any other ideas?

I am stuck and would appreciate any suggestions :-) Thanks for your help.


Solution

  • The Jupyter community offers nbviewer for full-featured viewing and sharing of Jupyter notebooks. GitHub is slow improving there offering but it doesn't support many features and you have found one.
    In fact up until a few years ago, GitHub didn't even attempt any sort of render of notebooks with more than a few cells of content. That was good in a way because new folks to the Jupyter ecosystem learned to use nbviewer sooner.

    Importantly, you can host your notebook at GitHub and point nbviewer at it and it will work. In your case you can continue to push to GitHub and use the nbviewer rendering to get the abilities you want. See specific examples demonstrating this below.
    There's no need to think about reformatting via a pre-commit hood because there's already an offered better solution that doesn't require any additional work.

    nbviewer is better for sharing as well because it lacks the GitHub framing that takes up significant screen space and is off-putting to non-coders. There's a link in the upper right side to the GitHub source for those looking to use the code.

    Please note the URL for the nbviewer view of the GitHub-hosted notebook file follows a pattern so you can point at it once you learn the pattern without needing to put the GitHub URL in the form at nbviewer to get the link.

    This is just one of the few features where nbviewer is substantially better and the view you generally want to be point out to people. (Plotly plots that are largely javascript-based remaining interactive despite being 'static' is generally one of the other popular features nbviewer supports that GitHub doesn't.)

    You'll see others use it to share 'static' views of Jupyter notebook files with others. For example, go here and click on the 'nbviewer' badge on the right, just under 'Clustergrammer2' heading.


    Compare and contrast this feature via the different renderings

    An example:

    Near top of cell #3 at https://nbviewer.org/github/fomightez/bear-classifier/blob/70ee6f58aa9d27fa0deaf9c387c6ca63f2d06bac/bear_classifier.ipynb
    looks good.

    vs.

    Near top of cell #3 https://github.com/fomightez/bear-classifier/blob/70ee6f58aa9d27fa0deaf9c387c6ca63f2d06bac/bear_classifier.ipynb. shows behavior that the OP doesn't like.

    Note these are just different renderings of the same GitHub-hosted content!


    Clarification

    OP states:

    "...and the content cannot be reused via copy & paste."

    I don't believe this is true. I think when you view the same code at either place, if you select it all in GitHub, even if you cannot see it, and copy the code text, I think you'll find you'll be pasting all the same code in your text editor. The problem can be though if it falls in a certain way at the end of what appears to be a line, some don't realize they should be insuring the selection of all the text code in the cell. Definitely overlooking there is possibly more text is a big issue.