Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loading a local *.csv file #8

Open
Fedorov-Artem opened this issue Aug 3, 2023 · 5 comments
Open

loading a local *.csv file #8

Fedorov-Artem opened this issue Aug 3, 2023 · 5 comments

Comments

@Fedorov-Artem
Copy link

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
Copy link

gramster commented Aug 3, 2023

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
Copy link
Author

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
Copy link

gramster commented Aug 4, 2023

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
Copy link

gramster commented Aug 4, 2023

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
Copy link
Author

Worked for me also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants