1

My issue is that if I use two functions, this code is not accepting the two input arguments.

I tried inserting and removing the click command and the click option from the second function as well, but I am always getting that the main app is asking for additional argument ( 2 need to be given ) or that the code is not executing the second function. ( "add_new_column" )

What am I'm doing wrong here?

import pandas as pd

@click.command()
@click.option('--infile', prompt='Your input TSV filename', help='Write your tab separated value filename.')
@click.option('--out', prompt='Your output CSV filename', help='Write your new comma separated value filename.')
def convert_tsv_to_csv(infile, out):
    """Converting a Tab Separated Value into a Comma Separated Value for given files in cli arguments"""
    df = pd.read_csv(infile, delimiter='\t')
    df.to_csv(out, sep=',')

# @click.command()
# @click.option('--out', prompt='Your output CSV filename', help='Write your new comma separated value filename.')
# def add_new_column(out):
#     """Adding a new column named  "price_edited"   """
#     df = pd.read_csv(out, delimiter=',')
#     # this line creates a new cloned column from price column, which is a Pandas series.
#     # we then add the series to the dataframe, which holds our parsed CSV file
#     df['price_edited'] = df['price']
#     # save the dataframe to CSV
#     df.to_csv(out, sep=',')


if __name__ == '__main__':
    convert_tsv_to_csv()
    #add_new_column()```

Second try:

import click
import pandas as pd


@click.command()
@click.option('--infile', prompt='Your input TSV filename', help='Write your tab separated value filename.')
@click.option('--out', prompt='Your output CSV filename', help='Write your new comma separated value filename.')
def convert_tsv_to_csv(infile, out):
    """Converting a Tab Separated Value into a Comma Separated Value for given files in cli arguments"""
    df = pd.read_csv(infile, delimiter='\t')
    df.to_csv(out, sep=',')


def add_new_column():
    """Adding a new column named  "price_edited"   """
    df = pd.read_csv(out, delimiter=',')
    # this line creates a new cloned column from price column, which is a Pandas series.
    # we then add the series to the dataframe, which holds our parsed CSV file
    df['price_edited'] = df['price']
    # save the dataframe to CSV
    df.to_csv(out, sep=',')


if __name__ == '__main__':
    convert_tsv_to_csv()
    add_new_column()
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Tomer
  • 13
  • 3
  • Your second try works on my machine. No errors. – Diablo Mar 10 '19 at 07:29
  • But it is not executing the "add new column" function – Tomer Mar 10 '19 at 11:14
  • The second example will never work as you are expecting, [see here](https://stackoverflow.com/a/52224691/7311767). Click is a tool for implementing command lines. What do you want your command line to look like? – Stephen Rauch Mar 10 '19 at 15:00
  • @StephenRauch, python runme.py --infile 1.csv --out 2.csv And I want to pass those two args to the second function as well ( I actually need to pass only the out in this specific second function case...) – Tomer Mar 10 '19 at 18:40
  • Ah I see. Just pass in the "add new column" function into your click function will do. See my working solution below. Just add 1 line: add_new_column(out) – Diablo Mar 11 '19 at 03:16

1 Answers1

0

Your confusion lies in the misunderstanding of what Click does. Click is used to parse the command line and then run functions as specified from the command line.

In the example you have shown here, you only need one function from the command line perspective. Which is to say that the program will do the same things each time it is run. So you only need one click function.

But I have two things to do!

To split the work into two function certainly can make sense, but those two functions need to be called from a single click function like so:

import pandas as pd
import click


@click.command()
@click.option('--infile', prompt='Your input TSV filename',
              help='Write your tab separated value filename.')
@click.option('--out', prompt='Your output CSV filename',
              help='Write your new comma separated value filename.')
def cli(infile, outfile):
    convert_tsv_to_csv(infile, outfile)
    add_new_column(outfile)


def convert_tsv_to_csv(infile, out):
    """Converting a Tab Separated Value into a Comma Separated Value
    for given files in cli arguments"""
    df = pd.read_csv(infile, delimiter='\t')
    df.to_csv(out, sep=',')


def add_new_column(out):
    """Adding a new column named  "price_edited"   """
    df = pd.read_csv(out, delimiter=',')
    # this line creates a new cloned column from price column, which
    # is a Pandas series.  We then add the series to the dataframe,
    # which holds our parsed CSV file
    df['price_edited'] = df['price']

    # save the dataframe to CSV
    df.to_csv(out, sep=',')

if __name__ == '__main__':
    cli()
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135