Search code examples
pythonpycharm

How to organize Python code into collapsable / expandable chunks?


In Pycharm, there's "code structurure" side bar which provides a tree to navigate through the code, but, it is only useful when the code has classes and methods and objects. If nothing of that is in code then it is useless. My question is: is there any way in which I dictate that this is a block, and I want to be able to collapse it and expand it? Something similar to Jupyter where the code is inherently divided to cells.

Currently, I'm doing this:

# ---------------------------------- chunck x blah blah -----------------------

EDIT: Most of comments say that I'm dumb and I don't know how to code efficiently and that I should use functions and classes. Guys, I know how to use those, that's not my question. Thanks.


Solution

  • PyCharm allows you to define 'code cells' when you have 'Scientific Mode' enabled. These code cells are collapsible and expandable. To quote from the PyCharm website:

    A “code cell” is a block of lines to be executed all at once in the integrated Python console. You can define cells simply by adding inline comments #%% to your regular Python files. PyCharm detects these comments and shows you a special run icon in the left gutter. Clicking this icon triggers the execution of a cell:

    enter image description here

    The only catch is that Scientific Mode and its code cell functionality are only available in PyCharm Professional Edition.