rstudio / rsconnect

Publish Shiny Applications, RMarkdown Documents, Jupyter Notebooks, Plumber APIs, and more
http://rstudio.github.io/rsconnect/
134 stars 83 forks source link

How to pre-install Python dependencies for reticulated project? #1052

Open TengMCing opened 8 months ago

TengMCing commented 8 months ago

Hi,

I have successfully deployed a reticulated project with the following app.R, but when I include tensorflow in py_install(), the app will take too long to start up because of the installation and shinyapps.io will shut it down.

library(shiny)
library(ggplot2)

reticulate::py_install(c("numpy", "Pillow"),
                       envname = "r-web_interface",
                       method = c("virtualenv"))
reticulate::use_virtualenv("r-web_interface")

PIL <- reticulate::import('PIL')
np <- reticulate::import("numpy")

server <- function(input, output) { 
  output$py_config <- renderText(capture.output(reticulate::py_config()))
}

ui <- fluidPage(textOutput('py_config'))

shinyApp(ui, server)

Is there a way to pre-install all these Python dependencies at build time?

It seems like there is a solution provided at https://solutions.posit.co/write-code/reticulate/, but when I deploy the app with the RStudio button, the published app will ignore all the Python dependencies (I have downloaded and checked the manifest.json file of the deployed app).

And if I use rsconnect::writeManifest() first to generate manifest.json and requirements.txt, then deploy the app with the rsconnect cli - rsconnect deploy manifest manifest.json, the deployed app will not be able to find a Python interpreter at .venv/bin/python.

GitHunter0 commented 3 months ago

Hey folks @aronatkins , @jjallaire , @hadley , being able to install python packages at building time (passing a requirements.txt or a conda environment.yml) is indispensable in my opinion since R lacks essential AI, deep learning and even image packages like kaleido ...

An alternative would be to Dockerize the app to be able to install whatever you want:

Therefore, the only simple viable solution today to deploy Shiny apps with reasonable scalability would be shinyapps.io (or Posit Connect but it is expensive and more suitable for larger enterprises). But since it does not allow python packages installation, what else can we do?

With that in mind, I believe most developers will not use Shiny (despite being a brilliant framework) because of these limitations. And our R community will continue to lose ground.

What are your thoughts about this?

Thank you

MatthiasReccius commented 1 month ago

I want to push this issue. I have spent several days and tried a plethora of workarounds trying to avoid having to install python packages at runtime. Without success. It appears that shinyapps.io does not cache venvs between session and rsconnect and shinyapps, in general, are doing their best to ignore them when they are in the uploaded files.

This must be an issue for many people since Python offers all kinds functionalities that native R does not.