hard wrap / force new line after n (e.g. 79) characters in editor

107 views
Skip to first unread message

Moritz Lürig

unread,
Apr 7, 2021, 4:24:16 AM4/7/21
to spyder
TL;DR can we have hard wrapping of lists to a specific length in the editor?

I am often working with lists that are longer than 79 characters, e.g. column names of wide pandas dataframes, which I sometimes need to hardcode in the script for reproducibility. I do this by printing the lists in the console, and then copying them back to the editor. however, in the console they wrap after each item, so that they take a lot of space vertically. I then have to copy them to another editor (e.g. notepad++), remove all linebreaks, an then  manually wrap them by adding line breaks. this is tedious, and I was wondering whether we could have this functionality in Spyder to make our scripts more readible, and adhere PEP formatting suggestions?

Kiko

unread,
Apr 7, 2021, 12:41:36 PM4/7/21
to spyd...@googlegroups.com
2021-04-07 10:24 GMT+02:00, Moritz Lürig <moritz...@gmail.com>:
> TL;DR can we have hard wrapping of lists to a specific length in the
> editor?
>
> I am often working with lists that are longer than 79 characters, e.g.
> column names of wide pandas dataframes, which I sometimes need to hardcode
> in the script for reproducibility. I do this by printing the lists in the
> console, and then copying them back to the editor. however, in the console
> they wrap after each item, so that they take a lot of space vertically. I
> then have to copy them to another editor (e.g. notepad++), remove all
> linebreaks, an then manually wrap them by adding line breaks. this is
> tedious, and I was wondering whether we could have this functionality in
> Spyder to make our scripts more readible, and adhere PEP formatting
> suggestions?

You could use black or yapf or.... to format your files. Spyder
shouldn't dictate how your code should look. It is better to separate
the IDE functionality itself from other needs the users could have.

I've made a spyder plugin to integrate black within Spyder but it was
for Spyder 3.x [1]. It shouldn't be difficult to update it to be
compatible to Spyder 5.x but I don't think I could find time in the
short term to learn the new plugin infrastructure in Spyder 5.x.

[1] https://github.com/kikocorreoso/spyder-black-formatter
> --
> You received this message because you are subscribed to the Google Groups
> "spyder" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to spyderlib+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/spyderlib/6d63e894-72f9-42be-9305-e799f4e111ffn%40googlegroups.com.
>

Moritz Lürig

unread,
Apr 8, 2021, 7:30:20 AM4/8/21
to spyder
> Spyder shouldn't dictate how your code should look. It is better to separate the IDE functionality itself from other needs the users could have.

I agree, that's why I think it should be optional, e.g. by using plugins like yours, or black. In fact, I just discovered that Spyder already has such functionality: on rightclick, you can format your selection with autopep8, but this doesn't format the lists to be less than 79 characters wide. 

In any case, I think that something like this, or your plugin, or a black plugin that works on sections or just the selected code would be a great feature for Spyder. I mean, producing legible, PEP-conform Python scripts is part of Spyder's mission, no?  

yfprojects

unread,
Apr 9, 2021, 2:30:12 AM4/9/21
to spyder
Hello there,

Spyder already has autopep8, yapf and black integrated. You can choose which one Spyder should use when formatting in the preferences. Go to Preferences > Completion and linting > Code style and formatting > Code formatting. The formatting of
yapf is what you are looking for. I actually wasn't able to get the yapf integration running but Spyder says that it should be working. If it doesn't work you can also use yapf from the command line.

> I do this by printing the lists in the console, and then copying them back to the editor. however, in the console they wrap after each item, so that they take a lot of space vertically.

My console within Spyder doesn't wrap after each element. It actually doesn't insert line endings at all.

Kind regards
yfprojects

Kiko

unread,
Apr 9, 2021, 4:07:50 AM4/9/21
to spyd...@googlegroups.com
2021-04-09 8:30 GMT+02:00, yfprojects <yfpro...@gmail.com>:
> Hello there,
>
> Spyder already has *autopep8*, *yapf* and *black* integrated. You can
> choose which one Spyder should use when formatting in the preferences. Go
> to *Preferences* > *Completion and linting* > *Code style and formatting* >
> *Code
> formatting*. The formatting of
> *yapf* is what you are looking for. I actually wasn't able to get the yapf
> integration running but Spyder says that it should be working. If it
> doesn't work you can also use yapf from the command line.
>
>> I do this by printing the lists in the console, and then copying them
> back to the editor. however, in the console they wrap after each item, so
> that they take a lot of space vertically.
>
> My console within Spyder doesn't wrap after each element. It actually
> doesn't insert line endings at all.
>
> Kind regards
> yfprojects

I was not aware of this. You can access this option going to 'Tools >
preferences > Completion and linting > Code formatting'

Good to know!!
> https://groups.google.com/d/msgid/spyderlib/b03bf411-1110-42ff-8eda-d8c0416c72abn%40googlegroups.com.
>

Moritz Lürig

unread,
Apr 12, 2021, 7:23:32 AM4/12/21
to spyder
that's cool!!

I figured that for me this feature is only useful when finishing up scripts or parts of modules. however, I often have scripts for data-analysis that I tinker a lot with, in which case I don't want the entire script formatted, but select parts that are "finished" and that I want to then format. do you think that "formatting on save" could be turned into another feature where you apply formatting on selection of the current script? wonder how hard this would be - worth a feature request?



Moritz Lürig

unread,
Apr 12, 2021, 7:27:54 AM4/12/21
to spyder
nevermind - this feature is already there:

when I select "black" in "formatting on save" it becomes the available option on right click - this is brilliant!

yfprojects

unread,
Apr 13, 2021, 2:02:02 AM4/13/21
to spyder
> when I select "black" in "formatting on save" it becomes the available option on right click - this is brilliant!

I don't what you mean with that. In my Spyder there is no difference in the interface when selecting `black` instead of `autopep8`.

> in which case I don't want the entire script formatted, but select parts that are "finished" and that I want to then format.

You can select some code in the editor and hit Ctrl+Alt+I. Only the selection is formatted in that case.

Kind regards
yfprojects
Reply all
Reply to author
Forward
0 new messages