Please make sure your output_table is a pandas.DataFrame

Hello KNIMErs,

I am trying to fetch the cookies of a website and then work with them, through the Python extension in KNIME.

If I execute the code inside the node, I get to see the cookie the way I want to, and there are no errors.
If I execute the node, trying to work with the output on a different node, I get the following error:

“Execute failed: Expected pandas.DataFrame, got <class ‘request.cookies.RequestCookieJar’> Please make sure your output_table is a pandas.DataFrame.”

So I investigated a bit deeper and it seems that for every Python node running, it is necessary to declare an “output table” (“output_table”), but not sure how to do so successfully.

Here’s my small code

import requests
from pandas import DataFrame
import pandas as pd

response = requests.get(‘https://iaas.service-now.com’)
print(response.cookies)

output_table = response.cookies

I’ve seen that some “= pd.DataFrame” code needs to be added somewhere before my “output_table” but not sure how to make it work.

Any ideas are truly appreciated.

1 Like

Hi @Barajas

I found interesting your question which made me curious about the Python -requests- library:
https://docs.python-requests.org/en/latest

I have come up with the following solution which I hope fulfills your needs:

from pandas import DataFrame
import requests

session = requests.Session()

x = session.get( flow_variables['URL'])

res = session.cookies.get_dict()

output_table = DataFrame({'cookies':res})

In summary, KNIME is happy to receive a JSON structure as data for the output table. Nicely, it generates in this way a table with the names of the cookies as index of the table rows:

I just verified too that the cookies of your website example really corresponded to those of the website, using the Firefox Inspect Element utility (How to Check Cookies on Your Website Manually - CookieYes):

The workflow example is here below:

20210914 Pikairos Please make sure your output table is a pandas dataframe.knwf (14.6 KB)

Hope this helps.

Best

Ael

5 Likes

Hi @Barajas,

I’m just curious whether the workflow I posted a week ago solved your problem ? Thanks for your feedback.

Best

Ael

4 Likes

This is awesome, it really does help. Sorry I couldn’t reply earlier. I will start playing around with that table it creates for the export since it covers my needs for the export of the python node, but not entirely fits what I need for an import of a different node (a single string containing the whole cookie), but I guess this would be much easier. Again, thank you for your helpful response.

3 Likes

Hi @Barajas

I’m glad it helped and thanks a lot for your kind comments :smiley: !

Could you please validate the solution :ballot_box_with_check: so that other users can easily find it :innocent: ?

Best,

Ael

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.