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()
5
  • Your second try works on my machine. No errors. Commented Mar 10, 2019 at 7:29
  • But it is not executing the "add new column" function Commented Mar 10, 2019 at 11:14
  • The second example will never work as you are expecting, see here. Click is a tool for implementing command lines. What do you want your command line to look like? Commented Mar 10, 2019 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...) Commented Mar 10, 2019 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) Commented Mar 11, 2019 at 3:16

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.