How to make section in PyCharm
Asked Answered
P

4

8

I have just moved to Python from R. In R you can use

#new section####

to have the 'new section' as title in the script section list. Then, you could easily find that block of script by hitting this title within "Jump to menu" (see figure). In this figure, the title is "reading data before 2013".

enter image description here

I have tried to find my answer here https://confluence.jetbrains.com/display/PYH/Code+folding and here https://www.jetbrains.com/help/pycharm/code-folding.html But I am lost in terms folding, class and....

How can I mimic this option in PyCharm?

Update: Thanks to @Robet, I could make a handy content by bookmarking (using F11) the title lines of each section. I then find the titles in favorites.
enter image description here

Priscella answered 14/3, 2019 at 13:2 Comment(0)
L
2

PyCharm also allows you to set anonymous and labeled bookmarks for navigating your source code. Their version of a jump-to menu is the Favorites window, which you can display using the View menu:

View --> Tool Windows --> Favorites

Linkboy answered 13/12, 2020 at 17:38 Comment(1)
#regions / #endregion are here to define code-structures (as the name 'region' says), therefore, to replace regions by Bookmarks is a bad coder suggestion ;-) The first time #region and #endregion were used was in November 2005 by Visual Studio 2005, and it's a shame that JetBrains still doesn't support it after almost two decades(!).Ambassadress
T
15

See Using code folding comments

For Python files:

 #region Description
 ...
 #endregion
Temikatemp answered 14/3, 2019 at 13:4 Comment(3)
I wish it worked this way. Unfortunately, it seems like it does not.Lindholm
@MartinWinter Care to be a bit more specific? Just tested this on the latest version of PyCharm and it still worksTemikatemp
Sure: First of all, you need to insert a space after the hash now to prevent a style warning, but that is only a minor issue. What I would like is for this to work like regions in Rider (C#), where the region titles appear in the Structure pane, allowing for quick scanning of the file’s structure and even for regions to be collapsed there too. In Xcode (Objective-C/Swift), MARK comments work similarly.Lindholm
L
2

PyCharm also allows you to set anonymous and labeled bookmarks for navigating your source code. Their version of a jump-to menu is the Favorites window, which you can display using the View menu:

View --> Tool Windows --> Favorites

Linkboy answered 13/12, 2020 at 17:38 Comment(1)
#regions / #endregion are here to define code-structures (as the name 'region' says), therefore, to replace regions by Bookmarks is a bad coder suggestion ;-) The first time #region and #endregion were used was in November 2005 by Visual Studio 2005, and it's a shame that JetBrains still doesn't support it after almost two decades(!).Ambassadress
F
2

Just to mention that the info concerning Code folding as linked by DeepSpace (can't comment on post) has changed place on Jet Brains website and is now here.

Also, although the accepted answer explains how to create a foldable block of code, it does not answer how to navigate through multiple blocks:

To fold blocks of code, select a code fragment and press Ctrl+Alt+T, from the popup menu select region...endregion Comments. Use Ctrl+Alt+. to navigate to the custom folding block.

Filipe answered 7/12, 2021 at 17:27 Comment(0)
I
0

In[] to start a new section in Pycharm

Immingle answered 10/9, 2024 at 13:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Scriabin

© 2022 - 2025 — McMap. All rights reserved.