Skip to content

Improve CLI feedback#485

Merged
nmaarnio merged 2 commits intomasterfrom
improve_cli_feedback
Mar 25, 2025
Merged

Improve CLI feedback#485
nmaarnio merged 2 commits intomasterfrom
improve_cli_feedback

Conversation

@mipeso
Copy link
Contributor

@mipeso mipeso commented Feb 25, 2025

I edited the CLI functions so that they now provide user with messages on their progress. I wanted to avoid hardcoding the messages in the CLI functions but at the same time to make sure they are handed out at correct times. Therefore I used the contextlib.contextmanager. @nmaarnio Let me know what you think.

@mipeso mipeso added the CLI Command-line interface related label Feb 25, 2025
@mipeso mipeso requested a review from nmaarnio February 25, 2025 11:33
Copy link
Collaborator

@nmaarnio nmaarnio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using context managers is a pretty good idea. However, I would make it a bit simpler than in your implementation and get rid of string parameters. I propose the following structure:

I propose using helper classes like these:

class ProgressLog():

   @contextmanager
   @staticmethod
   def reading_input_files():
       # typer.echo(f"Progress: {progress_at_start}%")
       typer.echo("Opening input files....")
       yield
       typer.echo("✅ Input files read\n")


   @contextmanager
   @staticmethod
   def running_algorithm():
       typer.echo("Running algorithm...")
       yield
       typer.echo("✅ Algorithm run succesfully\n")


   @contextmanager
   @staticmethod
   def saving_output_files():
       # typer.echo(f"Progress: {progress_at_start}%")
       typer.echo("Saving output files...")
       yield
       typer.echo("✅ Output files saved\n")


   @staticmethod
   def finish():
       typer.echo("✅ Algorithm execution finished succesfully\n")

class ResultSender():

   @staticmethod
   def send_dict_as_json(dictionary: dict):
       typer.echo(f"Results: {json.dumps(dictionary)}")

here is one example cli func with this proposed structure:

# NORMALITY TEST RASTER
@app.command()
def normality_test_raster_cli(input_raster: INPUT_FILE_OPTION, bands: Optional[List[int]] = None):
   """Compute Shapiro-Wilk test for normality on the input raster data."""
   from eis_toolkit.exploratory_analyses.normality_test import normality_test_array

   with ProgressLog.reading_input_files():
       with rasterio.open(input_raster) as raster:
           data = raster.read()
           if len(bands) == 0:
               bands = None

   with ProgressLog.running_algorithm():
       results_dict = normality_test_array(data=data, bands=bands, nodata_value=raster.nodata)

   ResultSender.send_dict_as_json(results_dict)

   ProgressLog.finish()

@nmaarnio
Copy link
Collaborator

I can take care of adjusting EIS QGIS Plugin to these new messages since I already started tinkering with that

@nmaarnio nmaarnio linked an issue Mar 19, 2025 that may be closed by this pull request
@mipeso mipeso force-pushed the improve_cli_feedback branch from 0b89ba2 to 597116d Compare March 20, 2025 13:08
Handle savepath printing in method saving_output_files
Copy link
Collaborator

@nmaarnio nmaarnio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

@nmaarnio nmaarnio merged commit bfa801a into master Mar 25, 2025
2 of 4 checks passed
@nmaarnio nmaarnio deleted the improve_cli_feedback branch March 25, 2025 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLI Command-line interface related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Redesign communicating with the user

2 participants