Getting images and code into a post here

I am pasting a jpg into this post (below) it shows a line of text. I am hoping that when I post the image will be uploaded.

  1. Otherwise, how do I get an image into a post?
  2. Can I paste or insert a Jupyter notebook here? (.ipynb file)

Oh good. The image posts. Now I will post my real question with a different title.
Sorry I had to bother you with this test.

Pasting an image works, but please don’t post screenshots to share code. It makes it impossible to tell if an exotic Unicode character has crept into your code, it doesn’t display well in some cases, and completely excludes those with vision impairment. Far better to copy the text of your code and/or output and paste it in a code block (using either the </> “Preformatted text” button in the editor toolbar, Ctrl+e, or manually adding triple backticks (```) on their own lines around your code).

For example:

In [1]: print('some output')
some output

In [2]: 1 + 2
Out[2]: 3

Also, perhaps most importantly, it makes it impossible for us to copy your code and run, test and modify it ourselves, which is often critical in confirming your problem and suggesting a solution.

And, as a minor sidenote, never ever use a JPEG image for a screenshot; use a PNG instead, which will both be higher quality and typically smaller size, as it is optimized for graphics rather than photographs.

What is a good way to get the display from Jupyter notebook into this format, including the In 3 Out 4 display , and maybe error messages?
Print Preview does a nice print preview, but selecting and copying that doesn’t get the formatting or the In 3 or the indentation.

This is a major weakness of Jupyter notebook as a format—the source is not plain text, and you are stuck within its silo’ed architecture rather than being able to use standard Python source code files like everyone else.

This, among many other reasons, I and many others recommend using a proper scientific focused IDE instead for any serious scientific Python work, e.g. Spyder (N.B. I am a volunteer core devs of it), which gives you most of the flexibility and convenience of Jupyter notebooks (and many others) including running code in your existing Jupyter kernels with all the same features, but using standard Python modules that you can easily share, reuse and build into larger libraries and applications.

Anyway, rant over. I’m not sure of an easy way to do so; there is nbconvert which will get the source code into a Python file, usually, but not the output. Its awfully hacky, but probably the simplest way is just copying the contents of your code cells into a Python file and running it.

If you want something that looks more like Jupyter, you can use Jupyter QtConsole (which we maintain and is the basis for Spyder’s own IPython Console), copy each cell’s contents in and run it, then you can simply copy and paste the output as plain text.

If you use an IDE like Spyder with code cells, copy each Jupyter cell into a source code cell, run it cell by cell with the “show cell source code” option enabled (at least in Spyder), then copy the source and output from the Console (which would produce the same result as copying and running it cell by cell in QtConsole, as that’s effectively what it uses under the hood).

I think that you are mistaken about Jupyter notebooks not being plain

text.

https://nbformat.readthedocs.io/en/latest/

I just downloaded this .ipynb file:

https://jupyter.brynmawr.edu/services/public/dblank/Jupyter%20Notebook%20Users%20Manual.ipynb

and not only did my browser offer to open it in my default text editor,

but after saving to disk and opening it, it was clearly a plain text

file.

Your other criticisms may or may not be valid.

Yes, ipynb files are XML which is are technically stored as “text” rather than encoded bytes, but by that standard, so too are MS Word DOCX files, (and LibreOffice Writer ODF files), as they are XML files too (or ZIPs of the same),that contain the text and its markup, just like Jupyter Notebooks. I don’t think anyone would call MS Word OOXMLs or LibreOffice ODFs “plain text files”, so neither too would one call ipynbs the same. They require interpretation by a compatible viewer to return a useful representation to the user, cannot be easily copied and pasted, and their plain text representation is not what is viewed, created and edited by the user, as opposed to their rendered form.

Also, the big HN thread that just happened about this very topic may be of interest.

Why there is problem with output if there is Python file? One can just run this file and get output.

If I have Jupyter notebook named test.ipynb then I would convert it to .py:

jupyter nbconvert --to python test.ipynb

Now I have test.py I can run it, get output etc. However, this is not much better than notebook for copying code as this contains all notebook in/out etc stuff as comments and lots of empty lines. This can be easily remedied by using text editor, I use Vim so I would open the file with it and remove empty lines and lines starting with # with simple command:

:g/^$\|\#/d

Now I have ‘clean’ Python code what I can directly copy and paste here (and to other sites as well).

At least for me it seems much easier to write two lines instead of dealing with screenshots. Easy for me to post and easy for others to copy/run and hopefully give some advice.

1 Like

Ah, thanks; my mistake, I forgot that nbconvert includes output. Its been quite a long time since I’ve used it, as I don’t really have to deal with Jupyter notebooks myself nowadays (at least for anything serious), despite being maintainer of a few of IPython/Jupyter’s deps and related packages. That does sound like an easier approach than the alternatives I’ve proposed, though still not ideal—but light years better than including an image, of course (which I wasn’t suggesting).