[GRASS-user] How to create a GRASS addon that returns something when using read_command()

Dear All,

I am using grass 7.8.4. I’m calling commands externally using the Grass Python Scripting Library in Python. I wanted to know when you create an addon in python how to end the addon script so that when you call that addon with read_command() it returns something. I would like to have something similar to what happens when calling read_command(g.gisenv) which returns the grass variables settings so that you can store them into a variable.

Thank you in advance for your answer.

Best,

Camille Morlighem

Hi Camille,

Great resources to start with are:

wenzeslaus/python-grass-addon: How to write a Python GRASS GIS 7 addon (github.com)

and

wenzeslaus/r.example.plus: An example of a GRASS GIS module for processing rasters (github.com)

If you want your module to return not maps or tables but output on stdout (what read_command() captures), just use the print() function in python.

Hope that helps to get started!?

If there is a module that has a UI that could be a good template for what you want to do, you can use e.g. “r.info –script” to get a script template for the UI and general content.

Cheers

Stefan

···

Dear All,

I am using grass 7.8.4. I’m calling commands externally using the Grass Python Scripting Library in Python. I wanted to know when you create an addon in python how to end the addon script so that when you call that addon with read_command() it returns something. I would like to have something similar to what happens when calling read_command(g.gisenv) which returns the grass variables settings so that you can store them into a variable.

Thank you in advance for your answer.

Best,

Camille Morlighem

Hi Stefan,

Thank you very much for your answer and the documentation! I was able to write my addon thanks to it :slight_smile:

Have a nice day,

Camille

Le jeu. 14 janv. 2021 à 21:39, Stefan Blumentrath <Stefan.Blumentrath@nina.no> a écrit :

Hi Camille,

Great resources to start with are:

wenzeslaus/python-grass-addon: How to write a Python GRASS GIS 7 addon (github.com)

and

wenzeslaus/r.example.plus: An example of a GRASS GIS module for processing rasters (github.com)

If you want your module to return not maps or tables but output on stdout (what read_command() captures), just use the print() function in python.

Hope that helps to get started!?

If there is a module that has a UI that could be a good template for what you want to do, you can use e.g. “r.info –script” to get a script template for the UI and general content.

Cheers

Stefan

From: grass-user <grass-user-bounces@lists.osgeo.org> On Behalf Of Cam Morlus
Sent: torsdag 14. januar 2021 21:23
To: grass-user@lists.osgeo.org
Subject: [GRASS-user] How to create a GRASS addon that returns something when using read_command()

Dear All,

I am using grass 7.8.4. I’m calling commands externally using the Grass Python Scripting Library in Python. I wanted to know when you create an addon in python how to end the addon script so that when you call that addon with read_command() it returns something. I would like to have something similar to what happens when calling read_command(g.gisenv) which returns the grass variables settings so that you can store them into a variable.

Thank you in advance for your answer.

Best,

Camille Morlighem