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.
Thanks for the bit of code.
When does the .Timer() stop? When the project i close/re-opened?