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()
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()