Alien
January 25, 2023, 10:53am
1
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.
@Alien ,
at least there is no error
Alien
January 25, 2023, 11:05am
3
Hahah!!
But what is is for?!
@Alien ,
i do not use print at all.
Alien
January 25, 2023, 11:07am
5
Me neither. Hence my question
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
Alien
January 25, 2023, 11:09am
7
ooo, didn’t know you could do that. Thanks
The print()
statement goes to the console in Dynamo - useful when you need to check things in loops
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
Alien
January 26, 2023, 11:55am
10
I’m in R21 mainly… and that doesn’t seem to work for me.
Is it just for R23?
Alien
January 26, 2023, 11:57am
11
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
Alien
January 26, 2023, 12:20pm
13
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
3 Likes
Alien
January 26, 2023, 4:22pm
16
Wait, you expect people to read stuff?
Srsly though, good to know! I’ll keep a look out when I use new Dyno
Am still struggling with the new look and the search box moving tho
2 Likes
You sound like the majority of my coworkers.
3 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