diff --git a/ReleaseNotes/release_notes.md b/ReleaseNotes/release_notes.md index 41a9208d..29ae8e1d 100644 --- a/ReleaseNotes/release_notes.md +++ b/ReleaseNotes/release_notes.md @@ -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 diff --git a/ostap/core/config.py b/ostap/core/config.py index 9eb41730..9d5db8d7 100644 --- a/ostap/core/config.py +++ b/ostap/core/config.py @@ -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' , @@ -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' ] @@ -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 # ============================================================================= diff --git a/ostap/core/default_config.py b/ostap/core/default_config.py index cc2f0469..697a49ab 100644 --- a/ostap/core/default_config.py +++ b/ostap/core/default_config.py @@ -28,6 +28,7 @@ # ============================================================================= quiet = False verbose = False +web = 'off' ## configuration files to read config_files = ( diff --git a/ostap/core/load_ostap.py b/ostap/core/load_ostap.py index c2d1807b..c0cf7a08 100755 --- a/ostap/core/load_ostap.py +++ b/ostap/core/load_ostap.py @@ -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 diff --git a/ostap/core/pyrouts.py b/ostap/core/pyrouts.py index a847a69a..2810b7ca 100755 --- a/ostap/core/pyrouts.py +++ b/ostap/core/pyrouts.py @@ -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 diff --git a/ostap/core/run_ostap.py b/ostap/core/run_ostap.py index 3e1fa69c..0930164c 100644 --- a/ostap/core/run_ostap.py +++ b/ostap/core/run_ostap.py @@ -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' , @@ -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' , @@ -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 @@ -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 : @@ -309,7 +327,9 @@ def _end_profile_ ( prof ) : _pr.enable() del _pr logger.info ( 'Profiling is activated' ) - + + + # ============================================================================= ## set ROOT into batch mode # ============================================================================= @@ -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 # ============================================================================= @@ -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 () diff --git a/ostap/plotting/canvas.py b/ostap/plotting/canvas.py index 6fed101e..d71a3019 100755 --- a/ostap/plotting/canvas.py +++ b/ostap/plotting/canvas.py @@ -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 @@ -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 # ============================================================================= @@ -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 diff --git a/ostap/utils/utils.py b/ostap/utils/utils.py index 8a5f7ab5..acd20e8e 100644 --- a/ostap/utils/utils.py +++ b/ostap/utils/utils.py @@ -368,7 +368,6 @@ def batch( batch = True ) : """ return Batch ( batch ) - # ============================================================================= ## context manager to keep the current working directory # @code