Run models on data table

I’m comfortable with Pandas. I’m just having difficulties in running my notebook code on a datable. I use the uplink to bind the app to the notebook, but need to know how to call the data into the notebook.

Here’s a snippet of code that I use, within a function (hence the return statements), to fill a Numpy array, from a MediaObject (report_blob below). In my case, the MediaObject contains a zipfile. This stores the CSV file in a much smaller space, but I have to unzip the CSV before use.

    if report_blob.content_type != 'application/zip':
        return { 'err': 'Container not found.' }
    # convert the media object into an in-memory file-like object:
    zip_as_in_memory_file = io.BytesIO(report_blob.get_bytes())
    z = zipfile.ZipFile(zip_as_in_memory_file)
    if 'reports.csv' not in z.namelist():
        return { 'err': 'Data file not found in container.' }
    # get the csv data as an object of Python type "bytes":
    with z.open('reports.csv') as csv_data:
        # use numpy to convert the CSV data into an in-memory array.
        # Pandas should have something similar.
        structured_array = np.genfromtxt(
            fname = csv_data, 
            delimiter=',',
            dtype=(int, float, float, int, int),
            names=True,  # 1st line contains column names
        )

@cardozo.luis Do you want to keep your data in your Jupyter Notebook or in the Anvil app? I recently answered a question about uploading a CSV file via Anvil and sending it to a Jupyter notebook that may be helpful: Send csv from Anvil to Jupyter

If you’re still having trouble loading the CSV to a Data Table, can you post your load_data server function? Also, how big is the CSV file?

Hi Brooke, I think this is on the right path, but I am still having trouble calling the data. I was able to load the csv into a data table, but with a run-time error (see above). I see the table on the app, run my notebook with your solution function (the notebook contains a first cell with uplink, then load some libraries, and a third cell with your solution), but still can not call to into Jupyter (the df is empty).

Load data server function:
‘’’ def load_data(file):
with anvil.media.TempFile(file) as file_name:
data = pd.read_csv(file_name)
print(data.shape)
for d in data.to_dict(orient=“records”):
# d is now a dict of {columnname → value} for this row
# We use Python’s **kwargs syntax to pass the whole dict as
# keyword arguments
app_tables.data.add_row(**d) ‘’’

the csv is (5000,14)

Regarding where I want the data, in principle, I would like in both mediums. I just need to understand how to organize it.

Hello Brooke. Feel that I’m almost there. I copied your solution into my notebook, but it seams I’m missing a step. I pasted the two functions below. Function A is in the server module and loads the csv into the data table, and function B is your suggestion to call the data table into the notebook. Could you help me with what I’m missing please? : )

A)
‘’’ @anvil.server.callable
def load_data(file):
with anvil.media.TempFile(file) as file_name:
data = pd.read_csv(file_name)
print(data.shape)

for d in data.to_dict(orient=“records”):
#d is now a dict of {columnname → value} for this row
#We use Python’s **kwargs syntax to pass the whole dict as
#keyword arguments
app_tables.data.add_row(**d) ‘’’

b)
‘’’ df = pd.DataFrame()

@anvil.server.callable
def pass_file(file):
with anvil.media.TempFile(file) as filename:
data = pd.read_csv(filename)
df.append(data) ‘’’

The pass_file function should be in your Jupyter notebook connected via the Uplink. Then, after someone uploads a CSV file in your Anvil app, you can call pass_file and pass in the uploaded CSV as an argument.

So you should have a function in your client something like this:

def file_loader_1_change(self, file, **event_args):
   anvil.server.call("pass_file", file)

But you can also use the Uplink to connect to your Data Tables.

Does that help?

It does, thank you very much.
So, on the server module, there’s a function to load a CSV into a data table, another function on the notebook to pass the CSV into it, and another function on the client module to pass the CSV to the notebook. I incorporated your last suggestion and ran the app without errors, just need to show the notebook’s output.
Is there a way to run the notebook remotely, cell by cell, and emailing its HTML?