False line wrapping to make code readable?

While doing a seaborn distplot, I came across the issue of having a ton of parameters that make the code way to long to display in a cell without scrolling. I want to to make use of going to the next line for each parameter set. I know that it is possible to enable line wrapping, though it’s a bit fiddly for a newbie like me. But I also note that the example I am working from displays the code as I want it to look. See the following reference at https://indianaiproduction.com/seaborn-histogram-using-seaborn-distplot/ (please scroll to find the best example under the heading sns distplot rugplot keyword arguments towards the end of the page).

So the question is: is that example display just for display (could be: copying and pasting as is gives me an unexpected indent error) or am I missing something? Is there a way to force a carriage return purely for display purposes without it being mistaken for an indent?

Thank you for your suggestions and comment.

all the best. C

Hi Chris,
First, pasting from the internet can often cause issues. It may simply be that in this case.

However, there are ways to use the editor smartly that don’t require you to play with line wrapping. And this doesn’t look like a case you’d need to adjust the line wrapping settings to achieve. You just hit return and it should handle the basic indenting. Sometimes it doesn’t though so don’t be shocked. However, what you are pointing to in that section ’ sns distplot rugplot keyword arguments’ requires some additional adjustments above & beyond the basic formatting if you want to make it look like that.

One thing I’ll do with more difficult to format code is to edit it in a separate text editor (one for coding like Sublime Text or VSCode or PyCharm IDE) and then paste into the cell. It does add additional steps that way, but it saves you from learning two separate ways to deal with formatting. I use my text editor for things beyond Jupyter notebooks, and so it is more of a long term investment.

Also you may want to look into is the code formatter Black. If you want to use it directly in Jupyter see here or here or here. (Also depending on which version of notebooks you are using see here although I haven’t tried that so YMMV.) Or use it as a plug-in for your favorite text editor.

1 Like

Hello fomightez,

Super comprehensive answer. Yes, I almost never get away cutting and pasting without some editing. I did , put everything on one line and tried to to return to lay out the code, but that always yields an indentation error in Jupyter Notebook. I’ll try it with an external editor but thank youvery much for all the other suggestions which I’ll explore right now. Lots of stuff I did not know in there.

Kind regards. c

1 Like