posit-dev / py-shinylive

Python package for deploying Shinylive applications
https://shiny.posit.co/py/docs/shinylive.html
MIT License
42 stars 4 forks source link

loading a local *.csv file #8

Open Fedorov-Artem opened 11 months ago

Fedorov-Artem commented 11 months ago

I couldn't understand from documentation how can I make a local file accessible to the application, so that it would be possible to read_csv and do things like that? Should I copy it to some location in shinylive folder? An what path should I provide for the script to work?

Some files in project directory can be accessed by the py-shiny app which works fine, but shinylive app gives file_not_found_error if I build a shinylive app from exactly the same project.

gramster commented 11 months ago

Came here with the exact same question. I could see perhaps one way would be to deploy the CSV files alongside the others and the pass an URL to read_csv; however, that would mean the code for Shiny Live would need to be different to the code for client/server deployment. So then the question would be: is there a way to tell at runtime whether you are using Shiny Live vs client/server? And a way to tell what the base URL is when running with Shiny Live?

Fedorov-Artem commented 11 months ago

I could see perhaps one way would be to deploy the CSV files alongside the others and the pass an URL to read_csv; however, that would mean the code for Shiny Live would need to be different to the code for client/server deployment. So then the question would be: is there a way to tell at runtime whether you are using Shiny Live vs client/server?

What do you mean by "deploy the CSV files alongside the others"? Where are you going to deploy it and what URL are you going to use? Note that using a URL to get a file is also a bit tricky as you need to import pyodide.http library and use it to make a request.

And a way to tell what the base URL is when running with Shiny Live? I guess something like this would work:

from pyodide.http import pyfetch
try:
df = pd.read_csv(<usual location for the shiny app>)
except:
df = pd.read_csv(pyfetch(<URL for the shinylive>))

P.S. I put the csv file into the same directory with app.py and it turns out that the app.json file actually contains all the data from the csv file. Not sure if this is a good practice and how can I import data from app.json into my python code, but all the contents of csv file are there. {"name": "my_csv_file.csv", "content": "index, and then all the other columns and all the data row by row from the csv.

gramster commented 11 months ago

Yes, your example is along the lines of what I was thinking, but ideally without hard-coding the entire URL; the code shouldn't have to depend on where the final deployment happens.

Interesting that the data is going in app.json. That certainly suggests there should be a way to access it. And explains why my app.json is 130MB.

gramster commented 11 months ago

Ah, I got it working; I thought this would be a no-op but its the magic that is needed:

    from pathlib import Path
    dataset = pd.read_csv(Path(__file__).parent / 'survey.csv')
Fedorov-Artem commented 11 months ago

Worked for me also.