• [PyCharm IDE]
Skip to end of metadata
Go to start of metadata

Why UML?

It can be really handy to have a schematical view of an application’s class hierarchy. PyCharm lets us quickly examine the structure of applications by generating UML class diagrams. This can help us understand the codebase we’re working on and navigate from diagram to code and back. We can even refactor from the UML diagrams! Let’s see how.

What this tutorial is about

This tutorial aims to demonstrate you how to use UML class diagrams in PyCharm. See the PyCharm online documentation for more details on general technigues of working with diagrams and reference information.

What this tutorial is not about

Studying UML is out of scope of this tutorial. Refer to product documentation for more information.

Before you start

Make sure that...

  • You are working with PyCharm version 2.7 or higher
  • At least one Python interpreter, version from 2.4 to 3.3 is properly installed on your computer. You can download an interpreter from this page.
  • UML Support and Python UML diagrams plugins are enabled. If they are not, enable them as described in the section Enabling and Disabling Plugins.

Preparing sample

Create a Python package Animals and the file Mammal.py inside (Alt+Insert→Python Package, Python File). Next, open this file for editing and add the following code:

Add more classes that extend the class Mammal: for example, you can create classes Carnivore and Herbivore.

Exploring structure via UML class diagram

Let's explore the structure of classes. For example, we would like to see the inheritance structure of mammals - what shall we do for this? Most easy: in the Project tool window, right-click the file Mammal.py, point to the node Diagrams on the context menu, and then click one of the available commands:

  • Show Diagram - when this command is selected, the UML Class diagram opens in the editor tab.
  • Show Diagram Popup - when this command is selected, the UML Class diagram opens in a separate popup window.

Next, select a specific class of mammals, for example, marsupials, and explore it. PyCharm shows a UML class diagram for this particular class:

By the way, you can do same, using the keyboard shortcuts: for an editor tab press Ctrl+Alt+U, for a popup window press Ctrl+Alt+Shift+ U

As you might have noticed, the diagram view by default is rather schematic. If we want to see more details, click, for example, and buttons on the diagram toolbar:

For a UML class diagram popup that lacks toolbar, use the context menu command Show Categories:

To learn more about the diagram toolbar and context menus, click F1, or refer to the page Class Diagram Toolbar and Context Menu.

Let's add notes to better understand the code base we’re working on. To add a note to a diagram, select the element you want to comment, and press Alt+Insert:

Press Enter, and then type the text of your note:

After clicking OK, the new note appears on the diagram:

Navigating between diagram and source code

While working with a UML class diagram, it is vital to be able to explore the underlying source code. With PyCharm, this is most easy. Just select a node element, or a member, and do one of the following:

  • Choose Jump to Source on the context menu
  • Press F4

PyCharm opens the corresponding file in a separate editor tab, with the caret resting at the class name (if a node element has been selected), or a member declaration (if a member has been selected):

What can we do directly from the UML class diagram?

Now, as we have a UML class diagram at hand, let's find out what can we do here, without moving to the source code or the Project tool window.

Find usages

PyCharm makes it possible to find usages of each node element directly from diagram. To do that, select a node element in diagram, and then choose Find Usages on the context menu, or press Alt+F7. The detected usages appear in the Find tool window:


If you want to rename a class or member, or move a class to another directory, you don't need to leave a UML diagram you are working with. PyCharm provides refactoring commands on the diagram context menu. Right-click a node element, point to Refactor, and then select one of the supported refactorings.

For example, if you want to rename a class, select it in the UML class diagram, and do one of the following:

  • Choose Refactor→Rename
  • Press Shift+F6

Then type the new name in the dialog box:

Add elements to a model

Let's now explore the relationships between the various species. This you can do also immediately in the UML class dialgram.

Press Space. The pop-up window appears, suggesting you to type the desired name:

Add Carnivore and see how it fits into the model:

Next, create more specific classes extending Carnivore or Herbivore on one hand, and the type of a mammal on the other hand. For example, the class Cow extends Herbivore and Eutherian. the class Tiger extends Carnovire and Eutherian, the class Duckbill extends Herbivore and Marsupial, the class TasmanianDevil extends Carnivore and Marsupial:

Let's explore the inheritance of these classes. Press Space, and add each class to the diagram:

By the way, if you click the toolbar button , the entire diagram will look more agreeable:

Remove elements from a diagram

Suppose you don;t want to see the Duckbill class any more, however, you would like to preserve it in the source code for future use. To delete a node element from diagram, select it, and press Delete:

Note that the class Duckbill still exists in the source code, though is not displayed in diagram any more:

Viewing changes as a diagram

What is interesting about the UML class diagrams is that when our code is under version control, PyCharm makes it possible to evaluate how our changes affect the application model. Whenever changes to a project under source control are made, they are reflected in the Changes tool window, which features the button .

You can also use the keyboard shortcut Ctrl+Alt+Shift+D. This will open up a visual representation of code that has been modified.

Your Rating: Results: PatheticBadOKGoodOutstanding! 40 rates
  • No labels