[GIS] Automatically save python scripts in QGIS

pyqgisqgisqgis-plugins

When writing python scripts, QGIS does not save them when the user saves his main project with the ctrl+S command or with Project->Save. Besides, when leaving QGIS, the program does not warn about unsaved changes in python scripts even if it warns about unsaved changes to the project and offers to save them.

Is there any option to change this behaviour?

I should add that using the dialogue option to save the project when quitting does indeed save the project, but not the python scripts. Those are simply lost.

Best Answer

If you want to save the current python script in the Python Editor each time you save your project, type the following into the console:

from PyQt4.QtGui import QDockWidget
def save_project():
    consoleWidget = iface.mainWindow().findChild( QDockWidget, 'PythonConsole' )
    consoleWidget.console.saveScriptFile()

QgsProject.instance().projectSaved.connect(save_project)

This essentially connects the projectSaved() signal to the saveScriptFile() function in the python console.


Credit to Germán Carrillo for his answer in this post in which the above answer was based on.



Edit:

One method of automatically saving all scripts is to slightly modify your processing console.py file which can be found in your QGIS directory. For example:

C:/Program Files/QGIS 2.18/apps/qgis/python/console/console.py

Find the def saveScriptFile(self): function and replace the first line:

tabWidget = self.tabEditorWidget.currentWidget()

with the following and indent everything after:

for i in range(self.tabEditorWidget.count()):
    tabWidget = self.tabEditorWidget.widget(i)

So that it looks like:

def saveScriptFile(self):
    for i in range(self.tabEditorWidget.count()):
        tabWidget = self.tabEditorWidget.widget(i)
        try:
            tabWidget.save()
        except (IOError, OSError) as error:
            msgText = QCoreApplication.translate('PythonConsole',
                                                 'The file <b>{0}</b> could not be saved. Error: {1}').format(tabWidget.path,
                                                                                                              error.strerror)
            self.callWidgetMessageBarEditor(msgText, 2, False)

Save the file.

Now we need to create a startup.py file in your /.qgis2/python/ directory, this file is loaded when QGIS starts up so it will run any code saved inside. Include the following code:

from qgis.core import QgsProject
from qgis.utils import iface
from PyQt4.QtGui import QDockWidget
def save_project():
    consoleWidget = iface.mainWindow().findChild( QDockWidget, 'PythonConsole' )
    consoleWidget.console.saveScriptFile()

QgsProject.instance().projectSaved.connect(save_project)

Save the file.

Now, hopefully, when you load QGIS and save a project, all opened scripts in the console editor should be saved.


(Note: I'm quite certain you can iterate through the opened scripts in the console editor but haven't found it yet. This would be a much better and less intrusive method as it avoids altering the source code.)

Related Question