TypeError: missing 2 required positional arguments

Hi, I cannot figure out why I get the TypeError. Appreciate your help. This is the error:

"TypeError: Analytics2() missing 2 required positional arguments: 'start_date' and 'end_date' at Form1, line 31"

here is the code:


    asset_id = (self.text_box_ID1.text, 
             self.text_box_ID2.text,
            self.text_box_ID3.text,
            self.text_box_ID4.text,
            self.text_box_ID5.text)
    
    benchmark_id = self.text_box_bmID.text
    risk_free_daily = int(self.text_box_rf.text) /252
    required_return_daily = int (self.text_box_rr.text)/252
    start_date = self.date_picker_from.date
    end_date = self.date_picker_to.date
    data_plot, analytics_table = anvil.server.call('Analytics2',
                                                   asset_id,benchmark_id,
                                                   risk_free_daily,
                                                   required_return_daily,
                                                   start_date,
                                                   end_date)

I have also tried below modification which didnt work and I got the same Error:

    start_date = str(self.date_picker_from.date)
    end_date = str(self.date_picker_to.date)

Thanks

Could you show the function signature of Analytics2?

def Analytics2 (asset_type,
                asset_id,
                benchmark_type,
                benchmark_id, 
                risk_free_daily, 
                required_return_daily,
                start_date,end_date):```

Thanks,

I can’t see anything wrong with my eyes. Can you share a clone?

sure, just give me a minute to find out how to share the clone as I have not done it before. Also, please be informed that the function (def Analytics2) is running from Jupyter Notebook. The function was working fine yesterday. But I am getting this error today!!!

Here is the clone link

Sorry, that app is the running app. I was looking for a clone of the code (click the :gear: and choose the share the code option). But yeah, if the issue is in the notebook, we’d need to see that code too (or the chunk that relate to the issue).

hi, please find the link below for you review.
https://anvil.works/build#clone:2NQ3OVWJ5WTFJES3=JT4KWBT6RHE5XHBKTACB2UVN

also below is the related part of the code in Jupyter NB.

def Analytics2 (asset_type,
                asset_id,
                benchmark_type,
                benchmark_id, 
                risk_free_daily, 
                required_return_daily,
                start_date,end_date):
   
    Date = pd.date_range(start = start_date, end = end_date, freq = 'D' )
    plot_data= pd.DataFrame(index = Date)
    plot_data.index.name = 'Date'
    plot_data = pd.DataFrame(index = Date)

Thanks

I count 8 parameters expected, and only 6 being sent.

1 Like

I removed the 2 extra arguments which were defined within the Jupyter NB. I still get the same error.

TypeError: Analytics2() missing 2 required positional arguments: ā€˜start_date’ and ā€˜end_date’

it seems for some reason anvil is not passing the ā€˜start_date’ and ā€˜end_date’ called from below code to the Analytics2().

Do you still get this after closing and reopening the notebook?

I closed and reopened notebook. I still get the same error.

I used the exact same inputs in notebook and in anvil app. The code runs perfectly fine in the notebook. However, with the same inputs from anvil app I get the above error.

I also tried amending the code in anvil to:
start_date = str(self.date_picker_from.date)
end_date = str(self.date_picker_to.date)

then I got the following error:
IndexError: single positional indexer is out-of-bounds

Wierd. It’s as if there’s another definition of Analytics2 marked as @anvil.server.callable, somewhere in your code. If there is more than one, then only the one registered last (most-recently) will be called.

what do you mean by this?
I think @p.colbert suggestion might be correct here -
If the call signature still has 8 positional arguments and you’re only sending 6 positional arguments to the function - then I think this must be the problem no?

from what I understand

# Client side
anvil.server.call('Analytics2', asset_id,
                                benchmark_id,
                                risk_free_daily,
                                required_return_daily,
                                start_date,
                                end_date)
# Server Side
@anvil.server.callable
def Analytics2 (asset_type,
                asset_id,
                benchmark_type,
                benchmark_id, 
                risk_free_daily, 
                required_return_daily,
                start_date,
                end_date):

What am I missing?

Hi, I found were the problem is by investigating the data sent from Anvil client to the notebook. However, I would be grateful of your help to resolve it.

I have 5 inputs for asset_id (see below) which the code in notebook iterates over them in a ā€˜for loop’. If I input all the 5 asset_id in the respected anvil app text_boxes the code works perfectly well. However, if one of the asset_id text_boxes is empty the code gives an error.

             self.text_box_ID2.text,
            self.text_box_ID3.text,
            self.text_box_ID4.text,
            self.text_box_ID5.text)
    
    benchmark_id = self.text_box_bmID.text
    risk_free_daily = int(self.text_box_rf.text) /252
    required_return_daily = int (self.text_box_rr.text)/252
    start_date = self.date_picker_from.date
    end_date = self.date_picker_to.date
    data_plot, analytics_table = anvil.server.call('Analytics2',
                                                   asset_id,benchmark_id,
                                                   risk_free_daily,
                                                   required_return_daily,
                                                   start_date,
                                                   end_date)

My question is how I should instruct Anvil client side not to send a non value to the asset_id tuple when there is no input.

Appreciate your help.
Thanks,

When asset_id is empty or None, what do you want to happen, instead?

asset_id is a tuple receiving input from 5 text_boxes. lets say:

text_box_1 = 1
text_box_2 = 2
text_box_3 = empty
text_box_4 = empty
text_box_5 = empty

currently my anvil client code returns:
asset_id = (1,2, , , ,)

In order for the code to work, I need to get:
asset_id =(1,2)

Thanks,

Ok… Anvil will transmit that tuple as a list, so you may as well convert it into a list to work with. That makes it mutable; that is we can modify it in-place. Something like the following should work:

asset_id_list = list(asset_id)
while None in asset_id_list:
  asset_id_list.remove(None)

Then you can pass asset_id_list in place of asset_id in the call to Analytics2.

hi, below worked and solved the problem . Thank you for the suggestion.

             self.text_box_ID2.text,
            self.text_box_ID3.text,
            self.text_box_ID4.text,
            self.text_box_ID5.text]
    
    while "" in asset_id:
     asset_id.remove("")```

Hi @m.iravani,

Glad to hear you solved the problem! In order to help other users find solutions quickly, you can mark questions as ā€œsolvedā€ by clicking the :white_check_mark: below the post.