-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support interactive graphics in python scripts #17587
Open
linev
wants to merge
5
commits into
root-project:master
Choose a base branch
from
linev:python_draw
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−26
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2da34c2
[pyroot] introduce TCanvas.Update pythonization
linev ffadc03
[pyroot] do not create extra thread for events processing
linev fc5e61b
Do not use PyOS_InputHook on windows
linev 798d7da
[pyroot] handle ProcessEvents() return value
linev 0c77f7c
Add documentation for TCanvas.Update pythonization
linev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tcanvas.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Author: Sergey Linev GSI 01/2025 | ||
|
||
################################################################################ | ||
# Copyright (C) 1995-2025, Rene Brun and Fons Rademakers. # | ||
# All rights reserved. # | ||
# # | ||
# For the licensing terms see $ROOTSYS/LICENSE. # | ||
# For the list of contributors see $ROOTSYS/README/CREDITS. # | ||
################################################################################ | ||
|
||
from . import pythonization | ||
|
||
def wait_press_windows(): | ||
from ROOT import gSystem | ||
import msvcrt | ||
import time | ||
|
||
while not gSystem.ProcessEvents(): | ||
if msvcrt.kbhit(): | ||
k = msvcrt.getch() | ||
if k[0] == 32: | ||
break | ||
else: | ||
time.sleep(0.01) | ||
|
||
|
||
def wait_press_posix(): | ||
from ROOT import gSystem | ||
import sys | ||
import select | ||
import tty | ||
import termios | ||
import time | ||
|
||
old_settings = termios.tcgetattr(sys.stdin) | ||
|
||
tty.setcbreak(sys.stdin.fileno()) | ||
|
||
try: | ||
|
||
while not gSystem.ProcessEvents(): | ||
c = '' | ||
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []): | ||
c = sys.stdin.read(1) | ||
if (c == '\x20'): | ||
break | ||
time.sleep(0.01) | ||
|
||
finally: | ||
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings) | ||
|
||
|
||
def _TCanvas_Update(self, block = False): | ||
""" | ||
Updates the canvas. | ||
Also blocks script execution and runs the ROOT graphics event loop until the <space> keyword is pressed, | ||
but only if the following conditions are met: | ||
* The `block` optional argument is set to `True`. | ||
* ROOT graphics are enabled, i.e. `ROOT.gROOT.IsBatch() == False`. | ||
* The script is running not in ipython notebooks. | ||
""" | ||
|
||
from ROOT import gROOT | ||
import os | ||
import sys | ||
|
||
self._Update() | ||
|
||
# blocking flag is not set | ||
if not block: | ||
return | ||
|
||
# no special handling in batch mode | ||
if gROOT.IsBatch(): | ||
return | ||
|
||
# no special handling in case of notebooks | ||
if 'IPython' in sys.modules and sys.modules['IPython'].version_info[0] >= 5: | ||
return | ||
|
||
print("Press <space> key to continue") | ||
|
||
if os.name == 'nt': | ||
wait_press_windows() | ||
else: | ||
wait_press_posix() | ||
|
||
|
||
@pythonization('TCanvas') | ||
def pythonize_tcanvas(klass): | ||
# Parameters: | ||
# klass: class to be pythonized | ||
|
||
klass._Update = klass.Update | ||
klass.Update = _TCanvas_Update | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need the usual copyright header here and I would also add some documentation that will end up in the Python Interface section of the docs https://root.cern.ch/doc/master/group__Pythonizations.html. Take for example https://github.com/root-project/root/blob/master/bindings/pyroot/pythonizations/python/ROOT/_pythonization/_tfile.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vepadulano
I apply your changes and rebase commits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are still missing the documentation as I suggested, see the
\pythondoc
block in_tfile.py
. I think it should describe the fact that theUpdate
method has been pythonized and a simple example usage.