The following Python code will, every 30 seconds, automatically update the data from the source for any QGIS layer with 'autoUpdate' in the layer name. Each time it updates, it will put the timestamp in the layer name, although you could leave that part out.
This is particularly useful for remote data sources like json-over-http or even local CSV files that might be changed by another program while the QGIS project is open.
Just paste the code into the QGIS Python console.
import threading
import datetime
import re
def autoUpdateLayers():
threading.Timer(30.0, autoUpdateLayers).start()
for layer in QgsProject.instance().mapLayers().values():
if 'autoUpdate' in layer.name():
print('autoUpdating layer: '+layer.name())
layer.dataProvider().forceReload()
layer.setName(
re.sub(
'autoUpdate.*',
'autoUpdated ' + datetime.datetime.now().strftime('%c'),
layer.name()
)
)
autoUpdateLayers()
CC0 "No Rights Reserved" So feel free to use, modify, or do whatever you want with this code.
I don't even recall writing this code, but I guess I did... so good question, @SBENGSDFI !
I think the Timer will continue to run until you close QGIS. Also, if you are just running the code in the Python console, it won't automatically be included as part of a saved project, so you would have to run it again manually in the console.
If you wanted it to automatically run when you open a project, you could set it up as a macro in the QGIS project properties > Macros tab, and then add it something like this:
I'll admit that I haven't really used python's Timer object except for this code snippet. But it looks like it has a
cancel()
method you could use.Also, this whole thing seems to be unnecessary with local files. I just tested with a local CSV, and it seems to instantly update automatically, even without enabling the "Watch file" option when adding a delimited text layer.