Send csv from Anvil to Jupyter

Hello,

I am new to web development, but I trying to build a process in my app where a I can upload a csv in Anvil and send the data back to Jupyter where I can read it in as a dataframe with Pandas. I can’t quite figure out how to structure the client-, nor the server-side code and could use help.

I’ve read the tutorials and they outline a process where the data is uploaded, transfered to jupyter, a model is run, and an output is returned to the client, all in one function. However, I just want to store the data as a dataframe in jupyter.

Another route I’ve tried is storing the csv as a media object in the Anvil data table; but again, I can’t figure out how to import the file and extract it’s contents in jupyter.

Any help would be greatly appreciated!

Hi @dcw22 and welcome to the forum!

I’m assuming you’re talking about this tutorial for building a front-end on a Jupyter notebook?

Unless I’m misunderstanding what you are trying to do, you could use a FileLoader (like in the linked tutorial) to upload your csv file to Anvil. Then when a file is uploaded, call a function in your Jupyter notebook using the Uplink and pass in the uploaded csv. That function in the Jupyter notebook could then use pd.read_csv to create a pandas DataFrame from the passed in file.

I hope that helps :slight_smile:

1 Like

hmm I’ll chalk this up to me being new at this, but I’m still a bit lost.

Using the tutorial as an example, I’ve written the following code:
Client Side
def file_loader_1_change(self, file, **event_args):
anvil.server.call(‘pass_file’,file)

Server Side/Jupyter
@anvil.server.callable
def pass_file(file):
with anvil.media.TempFile(file) as filename:
data = pd.read_csv(filename)

What I’m struggling with is how to access the variable “data” without having a “file” argument in my Jupyter code.

Thanks for the help!

As a side note, when pasting code blocks to the forums, you can wrap them in three back ticks, like this:

```python
print("this is a block of code") ```

This makes it a lot easier to read the code :slight_smile:

I’m not sure I fully understand what you are trying to do. Why don’t you want to have a file argument in your Jupyter code?

Oh that’s a good tip! the text definitely didn’t look like the code I dropped in.

But basically, I’m trying to create an upload page in my app. When a file is uploaded with the FileLoader, I’d like to send it Jupyter. Then I want to store that file as a dataframe within Jupyter.

However, using the code from the tutorial, I don’t see how I can can do this. This is because the image file gets sent to Jupyter, converted, modeled upon, and sent back to Anvil all in one function that occurs upon a change to the file loader. If I cut the Jupyter code off as above, nothing happens when I upload a file to the file loader. I can’t call “data” in Jupyter as it has only been defined within the function.

Is there a way to stop that process whereby the file is just stored as a variable in Jupyter? Do I have to write a different function in Jupyter? Is there a way to just call the temp file from Jupyter in the live app?

Again, apologies for the newbie questions :weary:

Don’t worry about being new! I understand your issue now, it was partly me over-simplifying your question.

In your Jupyter code, I think you can create a pandas DataFrame outside of your function, then append to it within the @anvil.server.callable function. Something like this:

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)
2 Likes