Python 3 - changing indents

Python 3 requires spaces in Dyno instead of tabs…

However, if I get my indents wrong and I want to add spaces it just deletes my text.

Before if my tabs were wrong I’d highlight the text and hit ‘tab’ and it’d all move over nicely.

I just tried the same thing with a space and it deleted my text.

How do I change the indent of several lines of text at once?

image

Tabs are still acceptable. I’ve heard of issues when mixing tabs and spaces, but I haven’t been able to replicate it.

Can you share an example case?

I ‘upgraded’ a node from py2 to py3… so it added the spaces for me.

~Then I C+P a chunk of code from 1 block (still on py2) to the py3 node… Realised it was the wrong indent and …

class CreateWindow(Form):
        def __init__(self):
                self.Name = "Create Window"
                self.Text = Moose"
                self.Size = Size(600, 400)
                self.CenterToScreen()
                self.value = None

                label = Label()
                label.Text = "blah blah blah"
                label.Parent = self
                label.Location = Point(20, 20)
                label.AutoSize = False
                label.Height = 30
                label.Width = 200
                label.Font = Font("Arial", 12)
                label.Font = Font(label.Font, FontStyle.Bold)
                
            self.textbox = TextBox()
			self.textbox.Parent = self
			self.textbox.Text = "moose"
			self.textbox.Location = Point(90, 45)
			self.textbox.Size = Size(140, 40)
			self.textbox.Font = Font("Arial", 18)
			self.textbox.Multiline = True
			self.textbox.CharacterCasing = CharacterCasing.Upper

it’s all spaces until the textbox

Also I just noticed that the def __ and everything after it seems to have been moved over another chunk. (That was done automatically using the 2 > 3 button. )

You may have had too many spaces in the original version that caused issues in the transfer. What version of Dynamo are you transferring from/to? Can you confirm there’s an issue with spaces and tabs with a simple example?

OK - original nodes in Revit 21 but opened in R23
here’s a small sample:

This is all using tabs

Then if I upgrade to py3 >>

You can see how far the indent has jumped - 24 spaces! That’s 6 tabs, which blatantly it wasn’t before.

I can’t easily move them back (or I can’t work out how to)

But you definitely have extra spaces in the original code. You can see that the indent under the definition is larger than the one under the class. This would have to be fixed.

OK - but with the original code (tabs) I can just highlight it, hold shift and tab and it moves (or just tab if I want it to go right)…

With spaces - how do I alter the indent for multiple rows?

I don’t think there’s a way to do that with spaces. You would have to use tabs.

@solamour @jacob.small

Is there a way to change the indent when using spaces?

Surely there must be as it’s pushing us to use spaces in py3?

I mean, I can C+P into Visual Studio… or into ChatGPT to fix, but that seems a bit of a naff workflow.

It’s not “forcing” you to spaces. It merely replaces tabs with spaces. Tabs and tabbing still work functionally the same. This isn’t the problem. The problem is that your original code in IP2 has too many spaces, so when it gets converted it results in extra tabs.

That’s one issue… but often I copy and paste other bits of code which has different indents.

It’s normally easy to adjust the indents.

What do you mean by “different indents”? If you copy one piece of code that uses tabs and one that uses spaces you should be fine from what I’ve seen. The new editor specifically fixes this error by replacing tabs with spaces like I mentioned. If you mean that you copy one section of code that uses 4 space tabs and one section that uses 5 space tabs then, yeah, you’re gonna have a problem. That’s where you’d have to clean up the code. The easiest way I’ve found is to just dedent and re-indent to force the same spacing. I don’t think there’s any way around it but I’d be curious to see.

Let’s say I want 8 spaces and I copy 5 lines of code that has zero spaces… Do I have to hit my space bar 40 times?

I’m asking how to de-indent or re-indent with spaces.

I’ve had issues mixing tabs and spaces in R21. Part of the code didn’t execute because of it.
I also had a warning I was mixing them in R23 earlier.

You either use spaces and deal with having to input every single space or you use tabs. In the latest version of the editor tabs get converted automatically, so you would just use the tab functionality while still maintaining your code in spaces.

What version do the tabs get updated to spaces automatically?

I believe that started in 2.17?

1 Like

So I have to wait. sigh

No fair :frowning:

Actually it looks like the old editor does that too. So I don’t know what to tell you. As long as you use the right number of spaces/tabs in an indent everything seems to work fine.

Sort of.

First up: In your Python preferences I recommend enabling the ‘Show Whitespace’ feature in Python. Now instead of guessing you should be able to know which are tabs and which are spaces.

Second up: you can remove and add tabs (or spaces depending on the editor) by highlighting blocks of text and using Tab to ‘indent’ or shift + tab to ‘Dede t’ the highlighted blocks.

And yes, tabs will still be tabs in some editors, but you can swap 40 spaces for 10 tabs by ‘dedenting’ and then ‘indenting’. It is painful, but this will shift entire blocks of text quickly.

Next up: I don’t recommend using the 2>3 converter unless you have a LOT of unsupported code. If you do, I recommend updating it outside of the Dynamo engine (ie: in visual studio). You can also copy text after upgrading in bulk (converting to spaces) if really needed. I recommend you copy the entire graph you want to copy from to a custom node if you do go that route (you can be in multiple custom nodes at once, but only one active workspace) and upgrade the Python in the custom node before copy-pasting anything. The bulk updater I have shared elsewhere can be modified to generate custom node versions of your entire library if it was desired.

Lastly, I recommend moving to CPython 3 and Revit 2022+, and no longer developing any code for Revit 2021 at this point. The new product line typically comes out around April, and that is only six months away. The organization and all projects should already be working on upgrading to ensure that everyone remains secure; and as developing for 2020 entails adding a LOT more work for something which shouldn’t be in high use, you are not getting any real ROI. Spend the cycles/effort upgrading projects instead.

2 Likes