Upload file to uplink local storage using file_loader

ABSTRACT:
So you have a script on your computer that you wrote in VS-CODE/PYCHARM, which uses some local files to interact with REST-API’s, DATABASES, and other scripts and modules. Now you want to front-end it with ANVIL, so your coworkers can use your script with their own files.

This feels like a pretty common first use case for Anvil. I spent 4 days looking for how to do this. I was about to give up on Anvil until I found the missing link.

Several other links involved alternate solutions or workarounds:

So many false trails… I explored Pandas, and considered rewriting the anvil.server code to expand the ws4py functionality to include file transfers, considered using google drive, but my company is markedly anti-google and would block it at the dns layer.

The answer is so simple. 5 Lines of code (beyond the uplink setup). AND FAST!

To transfer a file from the file_loader object on a client web form, to the local file system of your uplink server :

  1. Start with a new app, and drag a file_loader onto the form.

  2. Doubleclick the fileloader to open the Python code that activates when someone puts a file into the loader.

  3. Add a function call to your uplink server method, and pass it the file.


    In this case our Uplink Server function is named uplink_download, and the file_loader_1_change function already passes a file argument which we can reference in our call. Under the hood it appears that we are sending a byte stream from the client to Anvil which is then streamed to our uplink client as a variable to the function call.

  4. Now Click the Gear icon to bring up the form settings menu, select uplink and enable your uplink. Copy the privileged code string to clipboard or text file for step 7.

  5. Move to your Uplink Server and pip install anvil-uplink

  6. Create a folder in your Uplink Server file system to store the files.
    in this case I used local and .gitignore doesn’t sync with my repository.

  7. Create an app.py file on your uplink server or add this to your existing app.py. In that file add the following to setup the uplink:
    connect
    replace <your privileged code string> with the code from step 4

  8. Finally, define your function, which we referenced in step 3 as uplink_download.
    uplink_function
    in this case I’m just overwriting the same file for each upload. It has been suggested that giving the user the ability to set the file name would make your app insecure, so keep this in mind.

  9. Save and run your app.py

  10. Run your form, and upload files to your uplink server!

I found sending a 90k file was as fast as uploading a file to a webserver should be, even though in my case it was going to Anvil, turning around and coming right back to my local PC.

This opens up a world of possibilities for me, I can create config files with forms on Anvil and pass them as JSON files to my Uplink Server, provide no end of spreadsheet upload pages for easy user interfaces to systems that may only have Rest API’s. I’m sure some of you already knew how to do this, but I hope it prevents those out there like me from spending 4 days searching for the same answer.

Its actually pretty cool I can upload the file on my phone from my corporate onedrive account. Importing lease spreadsheet from my Iphone at lunch will certainly impress my friends in the IT department :sunglasses:

10 Likes

Thanks for sharing such a comprehensive how-to :slight_smile:

1 Like