This is a short tutorial of using Python together with DRAKON for building real programs.
A more advanced tutorial can be found in examples/Python folder in the DRAKON Editor .zip file.
Create a new file with DRAKON Editor.
Select Python as the programming language. In the main menu, go to File / File properties... and choose the right version of Python.
Create a new diagram called
Add your own code. In the main menu, go to File / File description... and add the following text:
=== footer ===
Generate the source code. In the main menu, go to DRAKON / Generate code or just press Ctrl-B (Command-B on Mac).
DRAKON Editor will create a new .py file and put it next to the .drn file. The Python code will be something like this:
def Hello(): #item 16 print("Hello, world!") return None Hello()
The formal parameters of the procedure are placed in the special Parameters icon, to the right from the Begin icon.
How to create a Parameters icon:
Add a new Action icon and put it next to the right side of the Begin icon.
Connect the Begin icon with the new Action icon with a horizontal line.
Add parameters, one parameter per line.
DRAKON Editor will generate the following code for the diagram:
def DoubleHello(firstGreeting, secondGreeting): #item 22 print(firstGreeting) #item 23 print(secondGreeting) return None
Note the item comments in the generated code. They tell which icon on the diagram the next piece of code is generated from.
To jump to the specific icon if you know its number, press Ctrl-I and enter the number. This is really convenient for debugging.
Sometimes it is necessary to add some hand-written code to the generated file. DRAKON Editor allows you to do that using the so called sections in the file description.
To edit the file description, go to File / File description...
The header section is added at the beginning of the generated file:
=== header === print("this code is put at the top") print("there can be several lines")
The footer section is added at the end of the generated file:
=== footer === print("this code is put at the bottom") print("this is the last line")
We were using the footer section in the Hello example above to actually run the procedure we created.
The section content is all text below the section header until the next section of end of file.
The text in the file description that goes before any sections is ignored.
The If icon produces the if statement:
The If icon in the diagram above produces this code:
#item 30 if length <= maxLength: #item 31 print("The text is short enough.") print("I can print it:") print(text) else: #item 34 print("The text is too long.")
It is possible to swap YES and NO exits of an if icon. Right-click on the if icon and choose Swap YES and NO.
Imagine you need to make a choice out of several options based on some value. In Python, you would write something like this:
def NumberToString(number): if number == 0: print("zero") elif number == 1: print("one") elif number == 2: print("two") else: print("a lot")
In DRAKON, you can use the Select and Case icons like this:
When an Arrow is added to an If or Select, a loop is created.
Do-Check-Do (hybrid) loop:
def PrintStrings(strings): for item in strings: print(item)
The DRAKON equivalent for the Python code above is:
Note that the keyword foreach is used instead of the standard for Python keyword.
The actual code that is generated for iteration over a collection depends on the Python version. The reason for that is the difference between iterator protocols in Python 2 and Python 3.
With DRAKON, it is possible to write C-style for constructs:
Which is a syntactically sugared version of the below picture:
There can be only one class per .drn file.
The class code should be put to the class section in the file description like this:
=== class === class MoneyWaster:
In order to turn a diagram into a class method, add this line:
at the beginning of the formal parameters list for the method.
Our class will look like this:
class MoneyWaster: def SetName(self, name): #item 121 self.name = name
It is possible to exclude the whole diagram from code generation.
If the first line of the formal parameters list is
then no code is generated for this diagram.
Its correctness, however, will still be checked against the DRAKON rules.