Sage-Bionetworks / projectLive_NF

Live view of projects of funding partners
2 stars 3 forks source link

Minor fixes for deployment changes to help with local run #175

Closed andrewelamb closed 2 years ago

andrewelamb commented 2 years ago

@jaybee84 @allaway. @brucehoff made some changes to how the app works to get it so that it deploys. This is the cause of the errors you are seeing here and here.

Two things you'll need to do, run the following code to create a python virtual env(as opposed to a conda env):

python3 -m venv virtual_env
chmod 755 virtual_env/bin/activate
source virtual_env/bin/activate
pip3 install synapseclient

You will also need to move "inst/oauth_config.yaml" to "oauth_config.yaml".

brucehoff commented 2 years ago

All: I'm interested in how supporting automated deployment to ShinyApps affects local builds and how to make both as easy as possible. If performing a local build is too cumbersome, let's talk about how to make it more streamlined.

andrewelamb commented 2 years ago

@brucehoff I think most folks at Sage are using miniconda as opposed to python virtual envs. We were up to this point for projectlive. I don't have any strong preferences.

brucehoff commented 2 years ago

@andrewelamb One option is to add a Dockerfile to this repo' that takes care of setting up the virtual env', as well as all other dependencies, required for a local build. One would then simply run the docker build and docker run commands to build and launch the app' locally.

jaybee84 commented 2 years ago

@andrewelamb Thanks! I have now created a python virtual env and installed synapseclient. But when I try to run projectLive locally, I am getting the following error:

Error : Python module synapseclient was not found.
Detected Python configuration:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
Evaluation error: Unable to access object (object is from previous session and is now invalid).

I confirmed that synapseclient is installed in my python virtual env. Any idea why projectLive cant find it?

andrewelamb commented 2 years ago

@jaybee84 https://community.rstudio.com/t/error-in-py-call-impl-unable-to-access-object/37147/3

You may need to restart rstudio?

Otherwise I would try starting with a clean clone of the repo. It maybe conflicts with the old conda env?

allaway commented 2 years ago

In case it's useful, reticulate::py_discover_config can be helpful for figuring out what python reticulate happens to be using. When you call use_conda or use_virtualenv without force = T, reticulate will silently fall back on a different python if it can't find the one you are requesting.

allaway commented 2 years ago

Sorry, I meant required = T, not force = T.

jaybee84 commented 2 years ago

Thanks @allaway .. this may be happening here: It seems that the virtual_env python is found but reticulate is falling back on the default usr/bin/python

reticulate::py_config()

python:         /usr/bin/python
libpython:      /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib
pythonhome:     /System/Library/Frameworks/Python.framework/Versions/2.7:/System/Library/Frameworks/Python.framework/Versions/2.7
version:        2.7.16 (default, Jun 18 2021, 03:23:53)  [GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy
numpy:          /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy
numpy_version:  1.8.0
synapseclient:  [NOT FOUND]

python versions found: 
 /usr/bin/python3
 /usr/bin/python
 /Users/jineta/virtual_env/bin/python

@andrewelamb I think the reticulate config is being called through the projectlive_modules. Would you like to add the code snippet in there?

andrewelamb commented 2 years ago

@jaybee84 I'm unclear on what you mean.

The called module is here.

jaybee84 commented 2 years ago

Please take a look here: https://github.com/Sage-Bionetworks/projectlive-modules/pull/53

I could not test it locally, but wanted to suggest a solution close to what I was referring to earlier. For some reason that I dont fully understand yet, adding this snippet to zzz.R which calls reticulate to make synapseclient available to the app did not work.

allaway commented 2 years ago

@jaybee84 , when I was poking around earlier I saw this was in globals.R in this repo, I think you might want to edit this one instead: https://github.com/Sage-Bionetworks/projectLive_NF/blob/a83f12753525c4b28c8fe88a81f1f15f0f4cbbd1/R/global.R#L6

andrewelamb commented 2 years ago

@jaybee84 That function you modified isn't used by the app, so changing it won't help.

The code for using reticulate and login is in these files: R/zzz.R R/global.R R/app_server.R

jaybee84 commented 2 years ago

Thanks @allaway and @andrewelamb ..

I edited both R/zzz.R and R/global.R ... neither helped..

I dont see reticulate being called in R/app_server.R ...

andrewelamb commented 2 years ago

It's being called in R/zzz.R:

`### This file loads synapseclient and makes it globally available to the app

Filename is zzz.R as per convention

synapse <- NULL

.onLoad <- function(libname, pkgname) { .GlobalEnv$synapseclient <- reticulate::import("synapseclient", delay_load = TRUE) }`

jaybee84 commented 2 years ago

Yes.. This is the first place I tried to modify (see comment here) .. unfortunately it did not work

andrewelamb commented 2 years ago

@allaway have you got this to work?

allaway commented 2 years ago

I have not tried...figured maybe too many cooks :)

@jaybee84 let me know if you want me to give it a shot on my laptop

jaybee84 commented 2 years ago

@allaway Sure, happy to have you try..

andrewelamb commented 2 years ago

Well you know (n = 2) > (n = 1)

andrewelamb commented 2 years ago

@jaybee84 Look here.

"I observed that neither the technique "use_python('path')" nor the tactic of Sys.setenv(RETICULATE_PYTHON = 'path') in .RProfile worked for me (of course I am sure it must have worked for others.)

In any case the line at terminal,

which -a python python3 did produce two paths to choose from (one for python2 and one for python3 installed on my mac), **so then I was able to create a ".Renviron" file in my home directory with this single line in it:

RETICULATE_PYTHON="/usr/local/bin/python3"** After I restarted RStudio, library(reticulate) activates the desired python3, and repl_python() opens a python3 interactive window, etc. etc."

jaybee84 commented 2 years ago

I got it to work on my end... The reason was the filepath.

In R/global.R adding required = TRUE was not working because the path called by reticulate::use_virtualenv(file.path(getwd(),venv_name)) was not routing to the correct env. It assumes that the virtual env is loaded in the current working directory which in my case it is not..

Once I hard coded the right path to the virtual_env.. it worked on my end

andrewelamb commented 2 years ago

Can you show me the exact modifcation you made?

jaybee84 commented 2 years ago

In R/global.R: replaced

reticulate::use_virtualenv(file.path(getwd(),venv_name))

with

reticulate::use_virtualenv("/Users/jineta/virtual_env", required = TRUE)

Note:

reticulate::use_virtualenv(file.path(getwd(),venv_name), required = TRUE)

does not find the right path

andrewelamb commented 2 years ago

@jaybee84 what directory were you in when you ran these commands:

python3 -m venv virtual_env
chmod 755 virtual_env/bin/activate
source virtual_env/bin/activate
pip3 install synapseclient

If you were in the directory with the app , ie. dir1/dir2/projectLive_NF, it should have created the virtualenv there

jaybee84 commented 2 years ago

Aha.. I was not in the projectLive_NF dir when I ran the commands to create the virtual_env. I generally create virtual envs in my home dir and did the same in this case too

andrewelamb commented 2 years ago

@jaybee84 Hopefully that's the issue :) Could you try that again and change the code back to

reticulate::use_virtualenv(file.path(getwd(),venv_name), required = TRUE)

and see if that works?

jaybee84 commented 2 years ago

yep.. that worked.. :)

andrewelamb commented 2 years ago

Great! Let's make sure it's working for @allaway before accpeting the PR.

allaway commented 2 years ago

Can someone share the oauth_config.yaml details? I don't have the client id or secret.

andrewelamb commented 2 years ago

They are in last pass under "Synapse Shiny local oauth client config" make sure everything is quoted like:

client_id: "0000" client_secret: "xxxxx" app_url: "http://127.0.0.1:8100"

allaway commented 2 years ago

Oh, I see, there is a generic config that folks are using for all apps running local. Interesting!

Anyway, I made the change to reticulate::use_virtualenv(file.path(getwd(),venv_name), required = T) and the app worked fine for me. Thanks!

andrewelamb commented 2 years ago

Yeah it's an OAuth thing, i know enough about ti to get it to work.