enthought / enthought/traitsui

etsdemo 'Run' button does not execute script in __main__ block

Open
#1,428 4 comments 0 reactions 0 assignees View on GitHub
component: etsdemo type: bug
Dominant language
Python
Stars
306
Forks
99
PR merge metrics
No merged PRs in 30d

Description

If one has a script that contains a block like this:
```
if __name__ == "__main__":
...
```
Trying to run this script from the `etsdemo` application by pressing the 'Run' button will not run that block, which is unexpected.

To reproduce, create a file like this:
```
if __name__ == "__main__":
print("Hello")
```
Then run this:
```
from etsdemo.main import main
main(
[
{
"version": 1,
"name": "Some name",
"root": ".", # or a path to a directory that contains the above test file.
},
]
)
```

That block is not run when one presses the 'Run' button.
![Dec-01-2020 18-03-35](https://user-images.githubusercontent.com/3673984/100778952-91291d80-33ff-11eb-9039-10147935b0e1.gif)

But if one changes the `"__main__"` to `"___main___"` (three underscores), then it would run:
![Dec-01-2020 18-01-11](https://user-images.githubusercontent.com/3673984/100778706-3ee7fc80-33ff-11eb-9325-79328b3d5141.gif)

From the snapshot of an old version of the application, this `___main___` is not a typo: It looks deliberate.
https://github.com/enthought/traitsui/blob/96f45ff9c51ff55b5724cbed2361b933f468c183/enthought/traits/ui/extras/demo.py

Looks like the [`run_code`](https://github.com/enthought/traitsui/blob/f7da6909ad1cecf457f61df5970c9281575ec61f/ets-demo/etsdemo/app.py#L462) method is serving two purposes:
(1) When the file is clicked, it loads the file to find this specially named variable 'demo'.
(2) When the Run button is clicked, it runs the file too.

For the first purpose, we don't want the block in `if __name__ == "__main__"` to run. For the second purpose, we do.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.