Skip to content

Commit

Permalink
Merge pull request #1566 from willend/main
Browse files Browse the repository at this point in the history
Makes "Pylab" launcher button available and functional across platforms.
  • Loading branch information
willend authored Feb 12, 2024
2 parents 686f5a7 + d7c0b8d commit 53cb312
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 36 deletions.
6 changes: 6 additions & 0 deletions cmake/Modules/InstallMCCODE.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ macro(installMCCODE)
install(PROGRAMS ${WORK}/support/${name} DESTINATION "${DEST_BINDIR}")
endforeach()

configure_file(
cmake/support/run-scripts/labenv.bat.in
work/support/${FLAVOR}-labenv.bat
@ONLY)
install(PROGRAMS ${WORK}/support/${FLAVOR}-labenv.bat DESTINATION "${DEST_BINDIR}")

# Python/Perl related batches special handling
foreach (name run.bat doc.bat resplot.bat plot.bat display.bat gui.bat guistart.bat plot-pyqtgraph.bat plot-matplotlib.bat plot-matlab.bat display-webgl.bat display-pyqtgraph.bat display-mantid.bat)
configure_file(
Expand Down
36 changes: 36 additions & 0 deletions cmake/support/run-scripts/labenv.bat.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@set BINDIR=%~dp0
@CALL %BINDIR%\\mccodeenv.bat
@ECHO OFF
@ECHO @FLAVOR@ Jupyter lab shell
@set INSTRUMENT=%1
@REM Check if we were called with an input file name?
@IF "%INSTRUMENT%"=="" (call :JupyLaunch "INSTRUMENT" ".")

@FOR /F "delims=" %%i IN ("%INSTRUMENT%") DO (
SET BASE=%%~ni
SET EXT=%%~xi
)

@IF "%EXT%"==".instr" (
@FLAVOR@-pygen %INSTRUMENT%
SET INSTRUMENT="%BASE%_generated.py"
goto :JupyText
) ELSE IF "%EXT%"==".py" (
goto :JupyLaunch
) ELSE IF "%EXT%"==".ipynb" (
goto :JupyText
) ELSE (JupyLaunch
call :JupyLaunch "INSTRUMENT" "."
)

@IF /I "%INSTRUMENT%"=="%%~G"
@IF "%INSTRUMENT%"=="" (call :JupyLaunch "INSTRUMENT" ".")
@ELSE ()

:JupyText
@jupytext --to ipynb %INSTRUMENT%
@SET INSTRUMENT="%BASE%_generated.ipynb"

:JupyLaunch
@jupyter lab %INSTRUMENT%
@goto :EOF
115 changes: 79 additions & 36 deletions cmake/support/run-scripts/labenv.in
Original file line number Diff line number Diff line change
@@ -1,36 +1,79 @@
@set BINDIR=%~dp0
@CALL %BINDIR%\\mccodeenv.bat
@ECHO OFF
@ECHO @FLAVOR@ Jupyter lab shell
@set INSTRUMENT=%1
@REM Check if we were called with an input file name?
@IF "%INSTRUMENT%"=="" (call :JupyLaunch "INSTRUMENT" ".")

@FOR /F "delims=" %%i IN ("%INSTRUMENT%") DO (
SET BASE=%%~ni
SET EXT=%%~xi
)

@IF "%EXT%"==".instr" (
@FLAVOR@-pygen %INSTRUMENT%
SET INSTRUMENT="%BASE%_generated.py"
goto :JupyText
) ELSE IF "%EXT%"==".py" (
goto :JupyLaunch
) ELSE IF "%EXT%"==".ipynb" (
goto :JupyText
) ELSE (JupyLaunch
call :JupyLaunch "INSTRUMENT" "."
)

@IF /I "%INSTRUMENT%"=="%%~G"
@IF "%INSTRUMENT%"=="" (call :JupyLaunch "INSTRUMENT" ".")
@ELSE ()

@:JupyText
@jupytext --to ipynb %INSTRUMENT%
@SET INSTRUMENT="%BASE%_generated.ipynb"

@:JupyLaunch
@jupyter lab %INSTRUMENT%
@goto :EOF
#!/usr/bin/env bash

if [[ 1 == 1 ]]; then
# Legacy McCode PATH setup
TOPENV="$0"

# Check if we are being called with a non-full path
if [[ $TOPENV != "/"* ]]; then
TOPENV="$PWD/$TOPENV"
fi

# Iterate down a (possible) chain of symlinks (macOS does not have readlink -f)
while [ -L "$TOPENV" ];
do
TOPENV=`readlink "$TOPENV"`
done
TOPENV=`dirname $TOPENV`
TOPENV=`dirname $TOPENV`
TOPENV=`dirname $TOPENV`

else
TOPENV="\$( cd -P \"\$( dirname \"\${BASH_SOURCE[0]}\" )\" && pwd )"
fi



ARCH=`arch`
UNAME=`uname -s`

# On macOS, shorten TMPDIR
if [[ ${UNAME} = Darwin* ]]; then
export TMPDIR=/tmp
OSXVER=`sw_vers -productVersion|cut -f 1 -d.`
fi

export PATH=$TOPENV/miniconda3/bin:$PATH

export @FLAVOR_UPPER@=$TOPENV/@DEST_DATADIR_COMPS@

export PATH=$TOPENV/@DEST_BINDIR@:$PATH
# Check if we are running with miniconda-provided python libs and set PYTHONPATH accordingly
if [ -d "$@FLAVOR_UPPER@/miniconda3" ]; then
#Activation hook for conda
_conda_bindir="$@FLAVOR_UPPER@/miniconda3/bin"
function conda() {
unset conda
eval "$(${_conda_bindir}/conda shell.bash hook 2> /dev/null)"
conda "$@"
}
conda activate $@FLAVOR_UPPER@/miniconda3
export PS1='(@FLAVOR@-@MCCODE_VERSION@/miniconda3) \W \$ '
else
export PS1='@FLAVOR@-@MCCODE_VERSION@ env \W \$ '
fi


if [ -d "$@FLAVOR_UPPER@/share/NCrystal/python/" ]; then
export PYTHONPATH=$@FLAVOR_UPPER@/share/NCrystal/python/:$PYTHONPATH
fi

# Finally, launch jupyter, potentially via $1 as input
export INSTRUMENT=$1
# Check if we got an input file or not
if [ -z "${INSTRUMENT}" ]; then
# Still empty? Look for most recent local .py or .ipynb file
export INSTRUMENT=`ls -rt *.py *.ipynb | tail -1`
if [ -z "${INSTRUMENT}" ]; then
# Still empty? Just open .
export INSTRUMENT="."
fi
fi

# Got an input, check if it has suffix .instr
if [[ "${INSTRUMENT}" == *".instr" ]]; then
@FLAVOR@-jupylab ${INSTRUMENT}
else
jupyter lab ${INSTRUMENT}
fi

0 comments on commit 53cb312

Please sign in to comment.