Calling custom Python function in Shiny webapp

Solved!
info-rchitect
Level 6
Calling custom Python function in Shiny webapp

Hi,

 

I have a Shiny webapp that takes user inputs.  I need to call a custom Python package function and pass the user inputs from the webapp and return the function value (in this case a SQL string) to the webapp.  Then I need to run that SQL from the webapp and return the dataset for download to CSV.

 

thx


Operating system used: Windows 10

0 Kudos
1 Solution
AlexT
Dataiker

Hi @info-rchitect ,

Using python code in rshiny app is possible with packages like: https://rstudio.github.io/reticulate/ 

Here is a sample 

library(reticulate)
use_python("/home/dataiku/dss_11/code-envs/python/py36test/bin/python")
py_run_string('

import dataiku
client = dataiku.api_client()
def update_global_var(variable_name, variable_value):

    global_variables = client.get_variables()
    print(global_variables)

    global_variables[variable_name] = variable_value
    client.set_variables(global_variables)
    print(global_variables)
')

main <- import_main()
main$update_global_var(variable_name='some_variable', variable_value='some_value')
py_capture_output(main$update_global_var(variable_name='some_variable', variable_value='some_value'))

 

 

View solution in original post

2 Replies
AlexT
Dataiker

Hi @info-rchitect ,

Using python code in rshiny app is possible with packages like: https://rstudio.github.io/reticulate/ 

Here is a sample 

library(reticulate)
use_python("/home/dataiku/dss_11/code-envs/python/py36test/bin/python")
py_run_string('

import dataiku
client = dataiku.api_client()
def update_global_var(variable_name, variable_value):

    global_variables = client.get_variables()
    print(global_variables)

    global_variables[variable_name] = variable_value
    client.set_variables(global_variables)
    print(global_variables)
')

main <- import_main()
main$update_global_var(variable_name='some_variable', variable_value='some_value')
py_capture_output(main$update_global_var(variable_name='some_variable', variable_value='some_value'))

 

 

info-rchitect
Level 6
Author

AlexT,

 

Have you seen any performance, scalability issues when exchanging information between R Shiny and Python?  The reason I ask is because, even though I love Python,  DASH in Python is much more cumbersome and less elegant than Shiny IMO.  However, R sucks for the dirty work under the hood.  Hence, I hope this partnership will work well, if performance is not degraded too much.

 

thx

0 Kudos