Skip to content

Commit

Permalink
1. add UseWeb & 'useWeb' contetx managers for setting the WebDispa…
Browse files Browse the repository at this point in the history
…ly into `ostap.plotting.canvas` module

  1. add command line option `-w/--web` optioon for `ostap` script to alolow defien Web-Distplay
  • Loading branch information
VanyaBelyaev committed Jul 10, 2024
1 parent 2c23e6a commit a5246b6
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 39 deletions.
4 changes: 3 additions & 1 deletion ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
1. add test for `ostap.stats.average`
1. reshullle a bit the code between `ostap.math.minimize` and `ostap.math.local_minimize`
1. add `sync_dirs` function into `ostap.io.files` module to syncronize the directories

1. add `UseWeb` & 'useWeb' contetx managers for setting the WebDispaly into `ostap.plotting.canvas` module
1. add command line option `-w/--web` optioon for `ostap` script to alolow defien Web-Distplay

## Backward incompatible

## Bug fixes
Expand Down
9 changes: 8 additions & 1 deletion ostap/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def _cp_str_ ( cp ) :
'Quiet' : str ( default_config.quiet ) ,
'Verbose' : str ( default_config.verbose ) ,
'Parallel' : 'PATHOS' ,
}
'WebDisplay' : default_config.web , ## ROOT.TROOT.Set/Get WebDisplay
}

## generic TCanvas configuration
config [ 'Canvas' ] = { 'Width' : '1000' , 'Height' : '800' ,
Expand Down Expand Up @@ -89,9 +90,11 @@ def _cp_str_ ( cp ) :
## sections
general = config [ 'General' ]


quiet = general.getboolean ( 'Quiet' , fallback = False )
verbose = general.getboolean ( 'Verbose', fallback = False )


# =============================================================================
## section with canvas configuration
canvas = config [ 'Canvas' ]
Expand All @@ -108,6 +111,10 @@ def _cp_str_ ( cp ) :
## section for Tables
tables = config [ 'Tables' ]

# =============================================================================
## Redefine webdidpay fron environment variable if set
general [ 'WebDisplay' ] = ostap_getenv ( 'OSTAP_DISPLAY' , general [ 'WebDisplay' ] )

# =============================================================================
# logging
# =============================================================================
Expand Down
1 change: 1 addition & 0 deletions ostap/core/default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# =============================================================================
quiet = False
verbose = False
web = 'off'

## configuration files to read
config_files = (
Expand Down
2 changes: 1 addition & 1 deletion ostap/core/load_ostap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# =============================================================================
# The Heart
# =============================================================================
## load zillions of decorations for ROOT-objects
## load zillions of decorations for ROOT-objects
import ostap.core.pyrouts ## NB: the most important line!
import ostap.io.zipshelve as DBASE

Expand Down
1 change: 1 addition & 0 deletions ostap/core/pyrouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
import ostap.utils.pdg_format

import ostap.plotting.canvas
import ostap.plotting.style

import ostap.fitting.minuit
import ostap.fitting.roofit
Expand Down
89 changes: 55 additions & 34 deletions ostap/core/run_ostap.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,16 @@ def __call__(self, parser, namespace, values, option_string=None):
from argparse import ArgumentParser
parser = ArgumentParser ( prog = 'ostap' )
#
group1 = parser.add_mutually_exclusive_group()
group1.add_argument (
## 1st exclusive group
group1 = parser.add_argument_group ( 'Control verbosity' , 'Control the general level of verbosity')
egroup1 = group1.add_mutually_exclusive_group()
egroup1.add_argument (
"-q" , "--quiet" ,
dest = 'Quiet' ,
action = 'store_true' ,
help = "Quite processing [default: %(default)s]" ,
default = _cnf.quiet )

group1.add_argument (
egroup1.add_argument (
"--verbose" ,
dest = 'Verbose' ,
action = 'store_true' ,
Expand Down Expand Up @@ -159,13 +160,6 @@ def __call__(self, parser, namespace, values, option_string=None):
help = "Do not use global Ostap context for the scripts",
default = True )
#
parser.add_argument (
'--no-canvas' ,
dest = 'canvas' ,
action = 'store_false' ,
help = "Do not create canvas",
default = True )
#
parser.add_argument (
'--no-color' ,
dest = 'Color' ,
Expand Down Expand Up @@ -200,24 +194,42 @@ def __call__(self, parser, namespace, values, option_string=None):
dest = 'build_dir' ,
help = "Build directory for ROOT" ,
default = '' )
#
group2 = parser.add_mutually_exclusive_group()
group2.add_argument ( '-i' ,
'--interactive' , dest='batch',
action = 'store_false' , default = False ,
help = "Interactive shell/start_ipython" )
group2.add_argument ( '-e' ,
'--embed' ,
action = 'store_true' ,
help = "Interactive embedded shell" )
group2.add_argument ( '-s' ,
'--simple' ,
action = 'store_true' ,
help = "Simple python shell" )
group2.add_argument ( '-b' ,
'--batch' ,
action = 'store_true' , default = False ,
help = "Batch processing: execute files and exit" )
#
## 2nd exclusive group
group2 = parser.add_argument_group ( 'Web Display' , 'Use Web/ROOT display, see ROOT.TROOT.(Set/Get)WebDisplay')
egroup2 = group2.add_mutually_exclusive_group()
egroup2.add_argument (
'-w' , '--web' ,
dest = 'web' ,
help = "Use WebDisplay, see ROOT.TROOT.(Get/Set)WebDisplay ",
default = '' )
#
egroup2.add_argument (
'--no-canvas' ,
dest = 'canvas' ,
action = 'store_false' ,
help = "Do not create canvas",
default = True )
#
## 3rd exclusive group
group3 = parser.add_argument_group ( 'Sesstion type' , 'General session type: interactive/embed/plain/batch...')
egroup3 = group3.add_mutually_exclusive_group()
egroup3.add_argument ( '-i' ,
'--interactive' , dest='batch',
action = 'store_false' , default = False ,
help = "Interactive shell/start_ipython" )
egroup3.add_argument ( '-e' ,
'--embed' ,
action = 'store_true' ,
help = "Interactive embedded shell" )
egroup3.add_argument ( '-s' ,
'--simple' ,
action = 'store_true' ,
help = "Simple python shell" )
egroup3.add_argument ( '-b' ,
'--batch' ,
action = 'store_true' , default = False ,
help = "Batch processing: execute files and exit" )

if not args :
import sys
Expand Down Expand Up @@ -286,7 +298,13 @@ def __call__(self, parser, namespace, values, option_string=None):
os.environ['OSTAP_CONFIG'] = cc

import ostap.core.config


# =============================================================================
## Web Display
# =============================================================================
if arguments.web :
ostap.core.config.general['WebDisplay'] = arguments.web

# =============================================================================
## use profiling ?
if arguments.Profile :
Expand All @@ -309,7 +327,9 @@ def _end_profile_ ( prof ) :
_pr.enable()
del _pr
logger.info ( 'Profiling is activated' )




# =============================================================================
## set ROOT into batch mode
# =============================================================================
Expand Down Expand Up @@ -339,12 +359,13 @@ def _end_profile_ ( prof ) :
ostap.core.build_dir.build_dir = bdir

del bdir, writeable , make_dir

# =============================================================================
## ostap startup: history, readlines, etc...
# =============================================================================
import ostap.core.startup


# =============================================================================
## import everything from ostap
# =============================================================================
Expand All @@ -356,11 +377,11 @@ def _end_profile_ ( prof ) :
else :
from ostap.core.load_ostap import *


# =============================================================================
## create default canvas
# =============================================================================
if arguments.canvas :
if arguments.canvas :

import ostap.plotting.canvas
logger.debug ( "Create the default canvas" )
canvas = ostap.plotting.canvas.getCanvas ()
Expand Down
83 changes: 82 additions & 1 deletion ostap/plotting/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
'draw_pads' , ## plot sequence of object on sequence of pads, adjustinng axis label size
'AutoPlots' , ## context manager to activate the auto-plotting machinery
'auto_plots' , ## ditto, but as function
'UseWeb' , ## constext manmager to use Web Displya
'useWeb' , ## constext manmager to use Web Displya
'setWebDisplay' , ## set WebDisplay, see ROOT.TROOT.SetWebDisplay
'use_pad' , ## context manager to modifty TPad
'KeepCanvas' , ## context manager to keep/preserve currect canvas
'keepCanvas' , ## context manager to keep/preserve currect canvas
Expand All @@ -32,7 +35,8 @@
from ostap.utils.cidict import cidict
from ostap.utils.utils import KeepCanvas, keepCanvas
from ostap.core.core import cidict_fun
from ostap.core.core import rootWarning
from ostap.core.core import rootWarning
from ostap.utils.utils import which
import ostap.plotting.style
import ROOT, os, tempfile, math
# =============================================================================
Expand All @@ -46,6 +50,83 @@
margin_left , margin_right ,
margin_top , margin_bottom )


# =============================================================================
#$ define WebDispla
# @see TROOT::SetWebDisplay
def setWebDisplay ( web ) :
"""Define WebDispllay
- see `ROOT.TROOT.SetWebDisplay`
"""
wlow = web.lower()
if not web : web = 'off'
elif 'root' == wlow : web = 'off'
elif 'chrome' == wlow :
if which ( web ) : pass
elif which ( wlow ) : web = wlow
elif which ( 'google-chrome' ) : web = 'google-chrome'
elif which ( 'google-chrome-stable' ) : web = 'google-chrome-stable'
ROOT.gROOT.SetWebDisplay( web )
return ROOT.gROOT.GetWebDisplay()

# =============================================================================
## @class UseWeb
# Context manager to redefien the web-display
# @code
# with UseWe ('chrome') :
# ...
# @endcode
# @see TROOT::GetWebDisplay
# @see TROOT::SetWebDisplay
class UseWeb(object) :
""" Context manager to redefien the web-display
>>> with UseWe ('chrome') :
>>> ...
- see `ROOT.TROOT.GetWebDisplay`
- see `ROOT.TROOT.SetWebDisplay`
"""
def __init__ ( self , web = "default" ) :

self.__web = web
self.__prev = None

## ENTER the context
def __enter__ ( self ) :
self.__prev = ROOT.gROOT.GetWebDisplay()
setWebDisplay ( self.__web )
return self

## EXIT the context
def __exit__ ( self , *_ ) :
if not self.__prev is None :
setWebDisplay ( self.__prev )

# =============================================================================
## Context manager to redefien the web-display
# @code
# with useWeb ('chrome') :
# ...
# @endcode
# @see TROOT::GetWebDisplay
# @see TROOT::SetWebDisplay
def useWeb( web = 'default' ) :
""" Context manager to redefien the web-display
>>> with useWeb ('chrome') :
>>> ...
- see `ROOT.TROOT.GetWebDisplay`
- see `ROOT.TROOT.SetWebDisplay`
"""
return UseWeb ( web )

# =============================================================================
import ostap.core.config as cnf
web = cnf.general['WebDisplay']
if web :
logger.debug ( 'Set WebDisplay to be `%s`' % web )
setWebDisplay ( web )

# =============================================================================
## list of created canvases
_canvases = []
# =============================================================================
## get the canvas
Expand Down
1 change: 0 additions & 1 deletion ostap/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ def batch( batch = True ) :
"""
return Batch ( batch )


# =============================================================================
## context manager to keep the current working directory
# @code
Expand Down

0 comments on commit a5246b6

Please sign in to comment.