QGIS-ResourceSharing icon indicating copy to clipboard operation
QGIS-ResourceSharing copied to clipboard

Add Python expressions support

Open ThomasG77 opened this issue 3 years ago • 3 comments

When people use expressions, they sometimes do not find specific functions. You have to make some of them using Python expressions.

Although expressions can be already published, is there any way to also push Python expressions? I missed the functionality or not available?

Triggered by recurring questions on the QGIS users mailing list like https://lists.osgeo.org/pipermail/qgis-user/2021-May/048919.html

Should be possible using the following Python function

@qgsfunction(args='auto', group='Layout')
def get_page_format(layoutname, pagenumber, feature, parent, context):
    layout = QgsProject.instance().layoutManager().layoutByName(layoutname)
    page = layout.pageCollection().page(pagenumber - 1)
    return QgsApplication.pageSizeRegistry().find(page.pageSize())

ThomasG77 avatar May 20 '21 22:05 ThomasG77

Thanks for you interest and taking time to report.

According to the documentation, expressions can actually be shared: https://qgis-contribution.github.io/QGIS-ResourceSharing/authoring/what-to-share.html

But I've never tried out this feature. Maybe I'm wrong. Could you confirm please?

Guts avatar Sep 21 '21 17:09 Guts

I can confirm my initial statement or I'm also wrong?! You can easily add new expressions using a JSON file like https://github.com/QGIS-Contribution/QGIS_Test-Resources/blob/master/collections/qgis_test/expressions/testexpressions.json in an expressions directory but it only act more or less as a shortcut for complex expressions you often need and does not always remember.

You can't add them to load magically some Python functions that will act as new expressions. I know I can set Python expressions within profile dedicated directory from

import os
from qgis.core import QgsApplication
os.join.path(QgsApplication.qgisSettingsDirPath(), 'python', 'expressions')

bit I'm not aware you can do it through QGIS-ResourceSharing

ThomasG77 avatar Sep 21 '21 22:09 ThomasG77

Can this be reconsidered? It seems like very reasonable idea and clear way to do that would be very desirable.

JanCaha avatar Mar 22 '22 20:03 JanCaha