1

I'm new to PyCharm tool and trying to figure out the rule about auto-generated code-folding blocks of comments in PyCharm (2020.2.1, Community Edition Build PC#202.6948.78).

Screenshot of my code: How can I show the code blocker in (A) instead of (B)?

In the screenshot, the code blocker appears in (B) which does not help organizing code view. I'd like it to appear in (A).

For this, I tried:

  1. changing number of "#" in different areas, but failed.
  2. added another line of numerous "#"s above line 11: code folding appeared between line 10 and 11
  3. chose line 12 and 13 and pressed Ctrl+".": This worked, but doing this every time would be bothersome.

Would anyone please provide me with some rule about when the comment folding starts and ends so that I simply follow the rule in my coding and set the default code-folding setting to reduce the visible part of my source code?

Thank you in advance.

Rich KS
  • 73
  • 6

1 Answers1

1

You have different options:

  1. Just use #%% or # In[] for dividing python code into different sections;
  2. Use the following code to make you code folding and show necessary content introduction:
#<editor-fold desc="The content to show">
print('Hello World')
#</editor-fold>

For folding code, you could use Code - Folding - Collapse All. You could also use shortcut which is showing in the menu which is Command Shift - for macOS.

HairyCat
  • 54
  • 1
  • Thank you for your answer. Firstly, your answer No. 2 worked! Regarding No. 1, I found "#%%" is supported by Professional version only while I'm using Community version and couldn't find a comment about "# In []". I tried with the following code: ##%% section 1 print (11) ##%% section 2 print (12) # In [section 3] print (21) # In [section 4] print (22) # print (3) # Additionally, while searching "#%%", I found there's a plugin making a code section by using ##, but it didn't do anything about folding – Rich KS Sep 05 '20 at 10:57
  • Glad to hear the second one worked. Sorry for the first method. I am using pro version. The first method worked for spyder before I started to use pycharm pro. Although it is not working for CE version, FYI, it is `#%%` with one star instead of `##%%`, also `# In[]` with space between `#` and `I`, without space between `n` and `[` instead of `# In []`. – HairyCat Sep 05 '20 at 21:49
  • I tried putting in different number of spaces but failed. Anyway, I found number of # does not affect closing of the No. 2, i.e., ############# works and only the description in "desc='description'" appears when collapsing the fold. This seems good enough for me. Thank you. – Rich KS Sep 05 '20 at 23:12