Indentation weirdness

106 views
Skip to first unread message

dbor...@willamette.edu

unread,
Feb 24, 2021, 6:25:33 PM2/24/21
to Glowscript Users
Hey Bruce,

Just wanted to highlight some weirdness that I ran into yesterday. I wrote a program for a lab that displays a bunch of arrows to (eventually) display the electric field of a charged ring. I wrote it in Glowscript, copied it to my clipboard in Windows 10, pasted it into Notepad and saved it to a .txt file to distribute it to students (attached). 

Somewhere in this process, some (but not all) of the indentations went from 4 spaces to tabs. I'm using a bunch of software that you didn't write so tracking down where this happened is not your problem. Just know that it happened and that the indents in lines 34 and 35 are tabs.

When students copied and pasted the code into Glowscript, the code ran, but with some weird results. In particular, it looks like the "theta" for loop in line 34 that is used in defining the arrow positions to append to list_of_arrows ran but didn't do anything EXCEPT in the final iteration when theta =  7*pi/4. The outer z loop seems to work fine and the robs loop further down seem to work fine.

I imagine this has to do with Python not liking to mix tabs and spaces for indentation (I just discovered that the use of tabs at all is considered bad practice in Python 3). I know some IDEs will yell at you if you try mixing tabs and spaces and Python's interpreter will throw an error (although apparently not consistently, which is a reported bug). I think it would be nice if Glowscript did so too, (or was able to work around it by making tabs and 4 spaces equivalent when transpiling the Python-esque Glowscript code to Javascript) because the failure mode was completely not obvious and essentially impossible for students to debug.

Even more weirdly, it seems like directly typing code into Glowscript with a mix of tabs and spaces that is identical to that in the .txt file does not lead to weirdness. It seems like typed in tabs immediately get converted into 4 spaces. Only cutting and pasting a tab into the editor leaves them as tabs.

Anyway, thought you might find this interesting...

Cheers,

Daniel
StarterCodeB1.txt

Bruce Sherwood

unread,
Feb 24, 2021, 8:42:46 PM2/24/21
to Glowscript Users
After downloading, I read up the program with Notepad and with Notepad++ on Windows and pasted into GlowScript. In both cases I found that every indentation was one or more tabs, and the program seemed to run, with this result:

image.png

Is this what I should see? I emphasize that I did NOT find a mixture of spaces and tabs. Moreover, when on line 36 I replaced the 3 tabs with 12 spaces, the program ran the same way. Next I changed line 37 from 3 tabs to 2 tabs plus 4 spaces, and again it ran the same. So I'm unable to debug a possible problem. 

I wonder whether it is possible that some of your students use GlowScript on a phone or an ipad, where instead of the Ace editor used at glowscript.org what is used is an editor that I had to build because Ace didn't (doesn't?) run on mobile devices.

Bruce

Joe Heafner

unread,
Feb 24, 2021, 8:46:16 PM2/24/21
to GlowScript Users


> On Feb 24, 2021, at 20:42, Bruce Sherwood <bruce.s...@gmail.com> wrote:
>
> I wonder whether it is possible that some of your students use GlowScript on a phone or an ipad, where instead of the Ace editor used at glowscript.org what is used is an editor that I had to build because Ace didn't (doesn't?) run on mobile devices.

Please pardon my intrusion, but this past summer I installed Ace on my website and it works just fine on my iPad.

https://www.sticksandshadows.net/editor/editor.html

It would be great to integrate this into GlowScript. I’ve no idea how to go about doing it though.

Joe Heafner
Sent from one of my Macs


Daniel Borrero Echeverry

unread,
Feb 24, 2021, 9:20:51 PM2/24/21
to glowscri...@googlegroups.com
Sorry I guess I wasn't super clear... All the OTHER indents other than lines 34 and 35 are 4 spaces. Lines 34 and 35 are tabs. (That's what I meant by mix of tabs and spaces)
Here's the code from the text file open in Notepad++ to show hidden characters. The indents are visible as orange arrows.

image.png

Here's some more detail on how to recreate the bug (or at least weirdness if it is not a bug). Windows 10, Chrome (v88.0.4324.150), straight www.glowscript.org editor.

1.) Create new program in GS
2.) Open txt file in Notepad. Cut and paste program into GS editor. If you run this you get the output that you sent me.
image.png

3.) Unindent lines 34 and 35 all the way using backspace. Reindent them using either the tab key or discrete spaces. Run the program and get this:

image.png

Note that the arrows that are shown in the first case are the ones that are generated in the last iteration of the middle (theta) loop, but they iterate correctly in robs (radial positions) and z (z positions), so for some reason the none of the iterations in theta except the last one occur. Also note that at any point after replacing the indents in lines 34 and 35 with new tabs, you cut and paste the code into Notepad++ (and I imagine Notepad), all the indents get copied as 4 spaces, so it seems to be a weird edge case where only code with tabs in it that gets pasted into Glowscript from an outside source runs into issues. Anything that you actually type in yourself never appears to run into it.

Here's the code after deleting the indents on lines 34 and 35 and replacing with new tabs in GS, then cutting the code and pasting it into Notepad++

image.png

--

---
You received this message because you are subscribed to a topic in the Google Groups "Glowscript Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/glowscript-users/Q2cECqLeaNs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to glowscript-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/glowscript-users/CA%2BWuaSceLkhUxK6dEuhzN59AjmuJSSq8K%3D8UkbzieR%3Dnkc2E_A%40mail.gmail.com.


--
Daniel Borrero
Assistant Professor
Department of Physics
Willamette University
900 State Street
Salem, OR 97301
U.S.A. 

Office: Collins Science Center 312
Office Hours: MW 1-2 and Th 11-12

Bruce Sherwood

unread,
Feb 24, 2021, 9:49:45 PM2/24/21
to Glowscript Users
Okay, I needed the additional information to see the effect. It looks like I need to check for tabs during the preprocessing that is done before sending the Python code to the RapydScript-NG transpiler, and convert each tab to 4 spaces. You don't get into trouble with code that isn't imported, because the GlowScript use of Ace is such that tab is automatically rendered as 4 spaces.

Of course if I convert every tab to 4 spaces you may get an error if the next statement has 3 spaces and no tab, but at least you will get an understandable error, and you'll also see the mismatch.

Bruce

Bruce Sherwood

unread,
Feb 24, 2021, 9:51:09 PM2/24/21
to Glowscript Users
Daniel, what was the source of the file that had tabs in indents?

Bruce

Daniel Borrero Echeverry

unread,
Feb 24, 2021, 10:04:00 PM2/24/21
to glowscri...@googlegroups.com
Also here's a more compact example: https://glowscript.org/#/user/dborrero/folder/Public/program/Test3

This is 
image.png

After some great sleuthing by Jed Rembold, it appears that Python 3 does some funny things with mixed tabs and spaces. I think that the last loop for some reason is interpreted as being at the same indentation level as the middle loop. So the middle loop is considered empty (which should throw an error but doesn't), runs to completion (that's why we only get the last value for theta) and then the last loop runs as if it were in the first indentation level instead of the second. How any of this maps to the Glowscript -> Javascript transpilation, I have no idea.

The code above gives a different output than this: https://glowscript.org/#/user/dborrero/folder/Public/program/Test4

image.png

Note that trying to run those from text files as regular python by running "python filename" in Anaconda shell (Python 3) both throw errors (first one throws and indentation error and the other throws a mixed tabs and spaces error).

I think the mixed tabs and spaces occurred because I modified some old file from Georgia Tech ca. 2009 (probably me, Matt Kohlmyer, or Danny Caballero wrote it) that had a bunch of tabs and changed things in 2021, but replaced some of the tabs with spaces. Regardless, I think that distributing code out to students to complete is common enough that this rather esoteric behavior should probably be addressed.


Bruce Sherwood

unread,
Feb 24, 2021, 10:11:14 PM2/24/21
to Glowscript Users
Obviously I can't fix indentation problems in the use of VPython with installed Python, but it should be quite easy for me to change tab to 4 spaces in indentations in the GlowScript environment. It is already the case that in preparing the Python code for the transpiler I examine and save the indentation to simplify the preprocessing of the rest of the line.

Bruce

Harlan Gilbert

unread,
Feb 24, 2021, 10:40:03 PM2/24/21
to glowscri...@googlegroups.com
Keep the tabs, please!  It is easier to add and delete these to alter the indentation, than to count.spaces!

On Wed, Feb 24, 2021 at 10:11 PM Bruce Sherwood <bruce.s...@gmail.com> wrote:
Obviously I can't fix indentation problems in the use of VPython with installed Python, but it should be quite easy for me to change tab to 4 spaces in indentations in the GlowScript environment. It is already the case that in preparing the Python code for the transpiler I examine and save the indentation to simplify the preprocessing of the rest of the line.

Bruce

--

---
You received this message because you are subscribed to the Google Groups "Glowscript Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glowscript-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/glowscript-users/CA%2BWuaSf24HLi9FZDg%2BEnZPW609s8sDiE%3DopmZMWTvft9O-4fBQ%40mail.gmail.com.
--
Harlan Gilbert
Co-Chair and Math, Physics, Computer Science, and Philosophy Teacher
Green Meadow Waldorf High School
Chestnut Ridge, NY 10977

Bruce Sherwood

unread,
Feb 25, 2021, 12:02:10 AM2/25/21
to Glowscript Users
There is no intention of removing the use of the tab key, which will continue to insert 4 spaces. 

Bruce

dbor...@willamette.edu

unread,
Feb 25, 2021, 1:47:30 PM2/25/21
to Glowscript Users
Yes, I understand that you have no ability to fix what Python does with indentations. I just meant to highlight a situation where Python throws an error but Glowscript doesn't, so that you were aware of it. Obviously there are reasons (either technical limitations or design decisions) for the two to sometimes behave differently and it is completely up to you to address these as you wish. 

As always, many thanks for your incredible service in developing and maintaining this amazing resource. Our students definitely see value in learning to program and even one semester of working with Glowscript does wonders for their perception of their ability to do so. Here's some data from our Intro Physics I course (where we do a good bit of Glowscript programming) that I think you'll be glad to see. The graphs on the left are from the beginning of the semester, the graphs on the right are from the end.


unnamed (2).png

Bruce Sherwood

unread,
Feb 25, 2021, 5:38:26 PM2/25/21
to Glowscript Users
The rise in comfort level is very gratifying. Thanks!

I'm puzzled that there is what looks to me like no significant change in perceptions of importance.

Bruce

Reply all
Reply to author
Forward
0 new messages