QGIS-Contribution / QGIS-ResourceSharing

QGIS Resource Sharing Plugin
http://qgis-contribution.github.io/QGIS-ResourceSharing/
GNU Affero General Public License v3.0
38 stars 18 forks source link

Add Python expressions support #203

Open ThomasG77 opened 3 years ago

ThomasG77 commented 3 years ago

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())
Guts commented 2 years ago

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?

ThomasG77 commented 2 years ago

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

JanCaha commented 2 years ago

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