Print in Python? NOT OUT!

Opening old graphs in R23 you get information to tell you that Python has been upgraded.

Reading through the info I notice it mentions print statements.

When would you use a print statement in Dynamo Python?

I am talking about print, not OUT.

Print doesn’t work in Dynamo like it does in pure Python.
image

@Alien ,

at least there is no error :wink:

Hahah!!
But what is is for?!

@Alien ,

i do not use print at all.

Me neither. Hence my question :laughing:

1 Like

In Dynamo for Revit you use TaskDialog to prompt something for the user:

import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
clr.AddReference("RevitAPIUI")
import Autodesk 
from Autodesk.Revit.UI import (TaskDialog, TaskDialogCommonButtons,
								TaskDialogCommandLinkId, TaskDialogResult)

title = 'Task Dialog Title'
dialog = TaskDialog(title)

# Properties
dialog.MainInstruction = 'Text Header'
dialog.MainContent = 'Text Content'
dialog.FooterText = 'Footer Text'
dialog.VerificationText = 'Verification Text'
# dialog.ExpandedContent = expanded_content

# Settings
dialog.TitleAutoPrefix = False
dialog.AllowCancellation = True

# Add Button
dialog.CommonButtons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Yes

# Set Default Button
dialog.DefaultButton = TaskDialogResult.None

# Add Command Link
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1,
					  'Command Button Text',
					  'Command Button Sub Text')
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2,
					  'Command Button Text 2',
					  'Command Button Sub Text 2')

result = dialog.Show()

if result == TaskDialogResult.Ok:
	print('Dialog was OK')
if result == TaskDialogResult.Yes:
	print('Dialog was Yes')
if result == TaskDialogResult.Cancel:
	print('Dialog was Cancelled')
if result == TaskDialogResult.CommandLink1:
	print('Button Was Pressed')
if result == TaskDialogResult.CommandLink2:
	print('Button 2 Was Pressed')
if dialog.WasVerificationChecked():
	print('Verification was Checked')

3 Likes

ooo, didn’t know you could do that. Thanks :slight_smile:

The print() statement goes to the console in Dynamo - useful when you need to check things in loops :slight_smile:

6 Likes

Other uses I have found with it:

  • When you need to debug something but don’t want to break a larger workflow by changing the out value.
  • When you want a way to ensure there is a log of the failure but do t want to build a logging system, as the entirety of the console is saved for each session. (Think try/ except where the except instead of returning the value to Dynamo prints it to the console/log)
  • When you want to use the same code in a non-Dynamo Python engine.
3 Likes

I’m in R21 mainly… and that doesn’t seem to work for me.
Is it just for R23?

To think of all the time I’ve spent removing the word print from real python stuff when putting it in Dyno!

And shoving in a load of, TEST lists to check my outputs.

In Dynamo for Revit go to View and check Show Console the Shortcut is Ctrl+Shift+Up

I’m not having issues accessing the console…
My console does not display those items.

I believe you need to be in a newer version.

2 Likes

We introduced this feature in Dynamo 2.8 - as per the Release Notes on the Github Wiki :slight_smile:

3 Likes

Wait, you expect people to read stuff? :expressionless:

Srsly though, good to know! I’ll keep a look out when I use new Dyno :slight_smile:
Am still struggling with the new look and the search box moving tho :frowning:

2 Likes

:nerd_face: :face_with_monocle: :grimacing: :roll_eyes: :face_with_diagonal_mouth: :sweat_smile:

I’m curious about your thoughts on the Search Box moving: Do you mean the Right-click search or Library? Or other? Theoretically the former two should be in the same place, just graphically different.

You sound like the majority of my coworkers. :laughing:

3 Likes

^^ This one.

2 Likes

FYI, if you really need this, we can redirect the stdout to the OUT variable with an in-memory file-like object

import sys
if sys.version_info.major >= 3 :
    from io import StringIO    
else:
    from cStringIO import StringIO
sys.stdout = StringIO()

alphabet = [chr(i) for i in range(ord('a'),ord('z')+1)]
def funtest():
    global dict_
    for numb, alpha in zip(range(1,60,1), alphabet):
        dict_[numb] = alpha
dict_ = {}
print("Start Test")
funtest()
print(dict_)
print("End Test")
sys.stdout.seek(0)
OUT =  sys.stdout.read()
5 Likes