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 :
-
Start with a new app, and drag a file_loader onto the form.
-
Doubleclick the fileloader to open the Python code that activates when someone puts a file into the loader.
-
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. -
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.
-
Move to your Uplink Server and pip install anvil-uplink
-
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. -
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:
replace <your privileged code string> with the code from step 4 -
Finally, define your function, which we referenced in step 3 as uplink_download.
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. -
Save and run your app.py
-
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