Run script on startup?

Hello

Is it possible to run automatically run a python script on startup that would launch a custom interface?

Regards

Derek

Hello,

if you run mantidplot (and I believe the workbench) with the command line argument -x it will immediately run the script on startup.

We’ve not tried it to start up user interfaces ourselves, so that is something you can try out.

Regards,
Nick Draper

A very late response but…

I have failed to find an example python script to open a MantidQt::API::UserSubWindow. Can you provide one?

Hi,
This should be everything you need.
I have saved a file called show_data_reduction.py to my Desktop with this contents:

"""Script to combine with mantidworkbench -x to launch a custom interface.
Note that this currently only supports the C++ interfaces.
"""
from mantidqt.interfacemanager import InterfaceManager
from mantidqt.utils.qt.qappthreadcall import QAppThreadCall
def show_interface(name):
    def impl():
        window = InterfaceManager().createSubWindow(name)
        window.show()
        return window
    window = QAppThreadCall(impl)()
    return window
# Example with Indirect Data Reduction. The string name needs to match the string
# returned by the static name() method on the UserSubWindow
# This could be customized to look at sys.argv to pull the name from the command line
show_interface("Data Reduction")

Then from terminal / command prompt, I have run:

cd Desktop
path/to/Mantid -x show_data_reduction.py

On different OS:

# Windows
C:\MantidInstall\bin\MantidWorkbench -x show_data_reduction.py

#Linux
/opt/Mantid/mantidworkbench -x show_data_reduction.py

#MacOS
/Applications/MantidWorkbench.app/Contents/MacOS/MantidWorkbench -x show_data_reduction.py

On an older version the executable may be launch_workbench.exe rather than MantidWorkbench and there is also a mantidplot equivalent.

Tell me if this works!

Yep this works a treat. Thanks

Ok so next question.

The python script window is left open after running the example script. Is it possible to close/hide the script window?

Let me check what you want to close?

  • Do you want the launch script to be removed from the script editor?
  • Do you want the script editor to be removed?
  • Do you want the Main Mantid Workbench window to be hidden/minimised??

The UserSubWindow is running in our operational control room as an overhead display. I would like to remove all non-essential distractions.

I would definitely like the script editor to be removed if possible. Minimising the main mantid window would be nice.

If you close the script editor [clicking the black x] (and probably also the IPython interpreter) and even drag the Workbench window to a smaller size, closing Mantid and reopening will allow Mantid to remember this layout. It can then be reversed in View>Restore Default Layout.

Does this help??

P.S. I’m just trying out ScreentoGIF and had a go for this, not sure I’ve quite got the hang of it yet:

The script editor and the UserSubWindow are floating. MantidPlot remembers which of the docked windows have been hidden but not the floating windows.

Starting the UserSubWindow from MantidPlot->Interfaces->UserSubWindow results in the window being docked but when I start MantidPlot with your script my UserSubWindow is floating.

Can these be docked?

Interestingly when the startup script is used the a MantidPlot->Windows menu does not appear with the option to “Change to floating/docked”

When I say interestingly what I perhaps should have said is"How can I dock the UserSubwindow with Mantid?"

Sorry, I forgot you were using MantidPlot. See if using a script like this works?

"""Script to combine with mantidplot -x to launch a custom interface.
Note that this currently only supports the C++ interfaces.
"""
from mantidqt.interfacemanager import InterfaceManager
from mantidqt.utils.qt.qappthreadcall import QAppThreadCall
from qtpy.QtWidgets import QApplication
def show_interface(name):
    def impl():
        window = InterfaceManager().createSubWindow(name)
        window.show()
        return window
    window = QAppThreadCall(impl)()
    return window
def hide_script_window_if_in_mantidplot():
    """If inside MantidPlot we try to find the
    script window and hide it. This is predicated on finding
    a top-level widget with MantidPlot: Python Window in the title
    """
    def impl():
        try:
            import _qti
        except ImportError:
            # assume not mantidplot
            return
        qapp = QApplication.instance()
        widgets = qapp.topLevelWidgets()
        script_window = None
        for widget in widgets:
            if 'Python Window' in widget.windowTitle():
                script_window = widget
                break
        if script_window is not None:
            script_window.hide()
    #
    QAppThreadCall(impl)()
# Example with Indirect Data Reduction. The string name needs to match the string
# returned by the static name() method on the UserSubWindow
# This could be customized to look at sys.argv to pull the name from the command line
show_interface("Data Reduction")
hide_script_window_if_in_mantidplot()

Thanks that does the trick. The script window is closed.

Last question I promise. Can the UserSubWindow be docked with Mantid?

Glad that works. Haha it’s great to have questions!

No as far as I’m aware they can’t be docked.

Actually that wasn’t my last question. I have two more.

Does mantid need to run to launch the UserSubWindow?

What I would really like is to relaunch the UserSubWindow if the user closes the window. Can this the scripted?

Thanks

Derek

Hi, Sorry for the slow reply.

You can launch a custom interface outside MantidWorkbench (or Plot) with mantidpython.

Here’s an example show_data_reduction.py script:

"""Script to launch a custom interface with mantidpython
Note that this currently only supports the C++ interfaces.
"""
from mantid.api import FrameworkManager
from mantidqt.interfacemanager import InterfaceManager
from qtpy.QtWidgets import QApplication
# Create Qt QApplication
qapp = QApplication(['Mantid Interface'])
# Create Mantid FrameworkManager
fmgr = FrameworkManager.Instance()
# Create interface
# This could be customized to look at sys.argv to pull the name from the command line
window = InterfaceManager().createSubWindow('Data Reduction')
window.show()
# Start the event loop
qapp.exec_()

I can’t remember what OS you are on, but to launch use one of these:

# Windows
C:\MantidInstall\bin\mantidpython.bat --classic show_data_reduction.py

#Linux
/opt/Mantid/bin/mantidpython --classic show_data_reduction.py

#MacOS
/Applications/MantidWorkbench.app/Contents/MacOS/mantidpython --classic show_data_reduction.py

As for restarting on exit. I think this could be scripted, although getting the correct logic might be complex. (A restart method and a proper exit method). This is something you could script, but we don’t have a quick solution to this.

Tell me if the script above works for you!

All the Best,
Daniel

Thanks for this.

An infinite loop in a bash script will do for a respawn script.

!/bin/bash
while :
mantidpython --classic show_data_reduction.py

Your example works perfectly. Unfortunately when I modify the show_data_reduction.py to use my UserSubWindow I get a core dump. I include the backtrace on the off chance that a clue is provided where the problem lies.

I guess a hint is the unhandled exception

Backtrace:-
Gtk-Message: 07:47:46.010: Failed to load module “canberra-gtk-module”
FrameworkManager-[Notice] Welcome to Mantid 5.0.0
FrameworkManager-[Notice] Please cite: Redirecting and this release: Mantid 5.0.0 Release Notes
DllOpen-[Error] Could not open library /home/codac-dev/work/mantid/build/bin/libMantidCurveFitting.so: /home/codac-dev/work/mantid/build/bin/libMantidCurveFitting.so: undefined symbol: _ZN6Mantid6Kernel13FileValidatorC1ERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS8_EEbb
DllOpen-[Error] Could not open library /home/codac-dev/work/mantid/build/bin/libMantidDataHandling.so: /home/codac-dev/work/mantid/build/bin/libMantidDataHandling.so: undefined symbol: _ZN6Mantid8Geometry24SampleEnvironmentFactoryC1ESt10unique_ptrINS0_28ISampleEnvironmentSpecFinderESt14default_deleteIS3_EE

********* UNHANDLED EXCEPTION *********
what(): CompositeBraggScatterer is already registered.

Backtrace:
/opt/Mantid/lib/mantid/api/…/…/…/lib/libMantidAPI.so(+0x1a0a29) [0x7f4548a0ea29]
/opt/Mantid/lib/mantid/api/…/…/…/lib/libMantidAPI.so(+0x1a0bfa) [0x7f4548a0ebfa]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x92ae6) [0x7f45593fcae6]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x91b49) [0x7f45593fbb49]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(__gxx_personality_v0+0x2a8) [0x7f45593fc4b8]
/lib/x86_64-linux-gnu/libgcc_s.so.1(+0x10573) [0x7f4559162573]
/lib/x86_64-linux-gnu/libgcc_s.so.1(_Unwind_Resume+0x125) [0x7f4559162df5]
/opt/Mantid/lib/mantid/api/…/…/…/lib/libMantidGeometry.so(_ZN6Mantid8Geometry25BraggScattererFactoryImpl18subscribeScattererINS0_23CompositeBraggScattererEEEvv+0x452) [0x7f454843ee42]
/lib64/ld-linux-x86-64.so.2(+0x10783) [0x7f455ab52783]
/lib64/ld-linux-x86-64.so.2(+0x1524f) [0x7f455ab5724f]
/lib/x86_64-linux-gnu/libc.so.6(_dl_catch_exception+0x6f) [0x7f455a66d51f]
/lib64/ld-linux-x86-64.so.2(+0x1481a) [0x7f455ab5681a]
/lib/x86_64-linux-gnu/libdl.so.2(+0xf96) [0x7f455a0e3f96]
/lib/x86_64-linux-gnu/libc.so.6(_dl_catch_exception+0x6f) [0x7f455a66d51f]
/lib/x86_64-linux-gnu/libc.so.6(_dl_catch_error+0x2f) [0x7f455a66d5af]
/lib/x86_64-linux-gnu/libdl.so.2(+0x1745) [0x7f455a0e4745]
/lib/x86_64-linux-gnu/libdl.so.2(dlopen+0x71) [0x7f455a0e4051]
/opt/Mantid/lib/mantid/kernel/…/…/…/lib/libMantidKernel.so(_ZN6Mantid6Kernel7DllOpen7openDllERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x2a) [0x7f4553432cda]
/opt/Mantid/lib/mantid/kernel/…/…/…/lib/libMantidKernel.so(_ZN6Mantid6Kernel14LibraryWrapper11openLibraryERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x1c) [0x7f4553468adc]
/opt/Mantid/lib/mantid/kernel/…/…/…/lib/libMantidKernel.so(_ZN6Mantid6Kernel18LibraryManagerImpl11openLibraryERKN4Poco4FileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x4a) [0x7f4553467a4a]
/opt/Mantid/lib/mantid/kernel/…/…/…/lib/libMantidKernel.so(_ZN6Mantid6Kernel18LibraryManagerImpl13openLibrariesERKN4Poco4FileENS1_13LoadLibrariesERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISD_EE+0x3c7) [0x7f4553467fa7]
/opt/Mantid/lib/mantid/kernel/…/…/…/lib/libMantidKernel.so(_ZN6Mantid6Kernel18LibraryManagerImpl13openLibrariesERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS1_13LoadLibrariesERKSt6vectorIS7_SaIS7_EE+0x138) [0x7f45534681c8]
/opt/Mantid/lib/mantid/api/…/…/…/lib/libMantidAPI.so(ZN6Mantid3API20FrameworkManagerImpl19loadPluginsUsingKeyERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES9+0x3b8) [0x7f4548a11388]
/opt/Mantid/lib/mantid/api/…/…/…/lib/libMantidAPI.so(_ZN6Mantid3API20FrameworkManagerImpl11loadPluginsEv+0xce) [0x7f4548a118ae]
/opt/Mantid/lib/mantid/api/…/…/…/lib/libMantidAPI.so(_ZN6Mantid3API20FrameworkManagerImplC1Ev+0x99) [0x7f4548a119c9]
/opt/Mantid/lib/mantid/api/…/…/…/lib/libMantidAPI.so(_ZN6Mantid6Kernel15SingletonHolderINS_3API20FrameworkManagerImplEE8InstanceEv+0x71) [0x7f4548a06491]
/opt/Mantid/lib/mantid/api/_api.so(+0x10d76a) [0x7f4548eb876a]
/opt/Mantid/lib/mantid/api/_api.so(+0x10f6a7) [0x7f4548eba6a7]
/usr/lib/x86_64-linux-gnu/libboost_python3-py36.so.1.65.1(ZNK5boost6python7objects8function4callEP7_objectS4+0x285) [0x7f4552ff9855]
/usr/lib/x86_64-linux-gnu/libboost_python3-py36.so.1.65.1(+0x21a18) [0x7f4552ff9a18]
/usr/lib/x86_64-linux-gnu/libboost_python3-py36.so.1.65.1(_ZNK5boost6python6detail17exception_handlerclERKNS_9function0IvEE+0x6b) [0x7f45530005db]
/opt/Mantid/lib/mantid/api/_api.so(+0xd43c3) [0x7f4548e7f3c3]

Do you have both a built version(/home/codac-dev/work/mantid/build/bin) and a released package(opt/Mantid/lib…) of Mantid on your computer? We’ve had problems on Ubuntu with them conflicting with eachother, and this looks very like the same problem. If you don’t need the release package version, then please uninstall it.

Otherwise, try setting the python path to the bin directory of your build.
export PYTHONPATH="/home/codac-dev/work/mantid/build/bin/:$PYTHONPATH"

I did indeed have a built version and a released package of Mantid. Following your instructions I still get a core dump but the problem now lies in my own code. I shall investigate.

Thanks for the help.