6

All the QGIS documentation I've seen on custom functions (used in expressions) says that the code is saved in the user's local expressions directory, e.g. ~/.local/share/QGIS/QGIS3/profiles/default/python/expressions in Linux. The same documentation typically says something like "remember to share your expression files if others need this functionality" (example)

It seems like the only way to share this code is for other users to manually copy it to their own expressions directory, however that seems inadequate. If multiple map developers share a version control system the onus is on them to manually copy any updates to custom functions to their expressions directory. It does not seem possible for expressions to be loaded from an alternate path, e.g. one relative to the .qgs file, and therefore automatically update each time a project is loaded.

I plan to create a Feature Request for loading custom functions from a relative path, but first wanted to check that I have understood the current situation correctly. Do all users of shared custom functions really need to manually copy the code to their expressions directory each time it changes?

6
  • You could create symlinks to the shared function files, or put the Expression directory under version control (and automate updates), or write a small python code to copy the files when the project opens etc.
    – JGH
    Mar 19, 2019 at 11:46
  • @JGH can you expand on "put the Expression directory under version control"? My expressions are already under version control but the problem is QGIS doesn't know to look there for expressions so they are never loaded when the project opens unless the user manually copies them to their local expressions directory.
    – tomfumb
    Mar 19, 2019 at 17:10
  • You would checkout the remote (svn, git, whatever) folder in ~/.local/share/QGIS/QGIS3/profiles/default/python/expressions, then either at given time interval or when an app opens (or when you manually do it), you refresh this local folder using the content of the remote folder
    – JGH
    Mar 19, 2019 at 17:31
  • Related gis.stackexchange.com/questions/195880/…. If you're able to run some python code at startup you can dynamically register your expression I guess.
    – YoLecomte
    Apr 9, 2020 at 20:42
  • 1
    @pathmapper apparently I forgot to do this before so I just did it now: github.com/qgis/QGIS/issues/39330
    – tomfumb
    Oct 12, 2020 at 12:43

1 Answer 1

1

If you write a custom function in a processing python script, you can then use it in your expression builder. It will appear among the other regular functions but won't appear in the function editor tab. This would be easier for you to share than copy-pasting your functions.

You can write a basic processing script with no input or output, just a display name, group name and all infos so that it's neatly sorted in your processing toolbox. Then add all your @qgsfunction() outside of the QgsProcessingAlgorithm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.