1

I have some custom python functions I have defined in QGIS which I use in print composer. I tried copying the python folder into the QGIS Server path but they do not get propagated. I have seen the following http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-Custom-python-expressions-in-QGIS-Server-td5362875.html but the answer seems vague to me. Any hints / documentation or that I could read on this anyone out there with an example that I could look at.

2

It depends how you have installed QGIS Server, you didn't mention it in your ticket.

If you are using Py-QGIS-Server for instance, it might be easier because it's a QGIS Server implementation in Python. I haven't tried. https://github.com/3liz/py-qgis-server

I am assuming you are not using Py-QGIS-Server, I have tried quickly by writing a quick QGIS Server plugin and it worked, so it is still easy. I have followed the link you gave more or less.

  1. Make a folder called ServerExpression for instance.

  2. In this folder, add a quick metadata.txt:

    [general]
    name=Server expression
    description=Expose Python expressions on QGIS Server
    about=Expose Python expressions on QGIS Server
    version=1.0
    qgisMinimumVersion=3.4
    author=Etienne Trimaille
    email=etrimaille@3liz.com
    server=True
    
  3. In this folder, add a __init__.py:

    from qgis.core import QgsMessageLog, Qgis, QgsExpression
    from qgis.utils import qgsfunction
    
    @qgsfunction(
        args='auto', group='Your group', usesGeometry=False, referencedColumns=[], helpText='Define the help string here')
    def your_expression(params, feature, parent):
        # UPDATE the qgsfunction above
        # ADD HERE THE EXPRESSION CODE THAT YOU WROTE IN QGIS.
        return params.upper()
    
    class ServerExpressionPlugin:
        def __init__(self):
            QgsMessageLog.logMessage('Loading expressions', 'ServerExpression', Qgis.Info)
            QgsExpression.registerFunction(your_expression)
    
    def serverClassFactory(serverIface):
        _ = serverIface
        return ServerExpressionPlugin()
    
  4. If in your layout, you have a label with

    [%your_expression('hello')%]
    

You will have a PDF with HELLO.

To make things easy, I have added all the code in the __init__.py. Of course, you can make your Python files listing your expressions, to make it easier to copy paste between Desktop and Server.

| improve this answer | |
  • Thanks, I will try it. I am using docker to run qgis server. Yes the 3liz docker container – kartoza-geek Mar 27 at 16:59
  • I have created the server plugin in QGIS plugin path but the functions do not get registered. I am using 3liz/qgis-map-server:3.10 – kartoza-geek Mar 27 at 18:00
  • Do you have any server plugin already which are working? – etrimaille Mar 30 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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