Developing Applications¶
SolveBio recommends that you develop apps with the tools and frameworks you feel most comfortable with. For R programmers, that usually means Shiny (RStudio), and for Python programmers we recommend Dash (Plotly).
R Shiny and SolveBio¶
Shiny is an R package that makes it easy to build interactive web apps straight from R. You can use it to build standalone web apps and dashboards. Shiny can also be extended with custom CSS and JavaScript. The best way to get started with Shiny is to follow the Shiny Tutorial. Shiny is backed by a large community of passionate developers and there is a wealth of knowledge, examples, and components readily available for use.
If you're creating a new Shiny app, we recommend starting from our SolveBio Shiny Example.
To integrate SolveBio into an existing Shiny app use the Protected Server R function. First, make sure you have the latest SolveBio package for R. Open up an R shell and run the following command:
# Install the SolveBio package install.packages("solvebio") # Install Shiny install.packages("shiny") # Install ShinyJS (optional, for token cookie storage) install.packages("shinyjs")
Next, in your app.R
file, wrap the server
by solvebio::protectedServer
. Make sure to plug in the client ID you got by creating your app on SolveBio.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | library(shiny) library(shinyjs) library(solvebio) # Set Shiny to use port 3838 (for development only) options(shiny.port = 3838) server <- function(input, output, session) { output$current_user <- renderText({ # To use the current user's SolveBio credentials, # retrieve the `env` from the session: env <- session$userData$solvebio_env # Pass the env to any SolveBio R function: user <- User.retrieve(env=env) paste("Logged-in as: ", user$full_name) }) } ui <- fluidPage( # Optional code for token cookie support shiny::tags$head( shiny::tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.0/js.cookie.js") ), useShinyjs(), extendShinyjs(text = solvebio::protectedServerJS(), functions = c("enableCookieAuth", "getCookie", "setCookie", "rmCookie")), # Your UI code: titlePanel("Welcome to your SolveBio Shiny app!"), mainPanel(textOutput("current_user")) ) # Wrap your base server and return a new protected server function # Setting client_secret is optional but will encrypt OAuth2 tokens in browser cookies protected_server <- solvebio::protectedServer(server, client_id="YOUR CLIENT ID", client_secret=NULL) # On the last line of the file, declare your Shiny app with the protected_server shinyApp(ui = ui, server = protected_server) |
To run the app locally, run the code through RStudio or run the following command in your shell:
R -e "shiny::runApp(port=3838)"
Now, open http://127.0.0.1:3838 in your web browser.
Please contact SolveBio support to retrieve the client_secret
for your app.
Python Dash and SolveBio¶
Dash is a Python framework for building interactive web apps and dashboards right from Python (no JavaScript required). It is built on top of Plotly.js, React, and Flask, and makes it easy to tie together modern UI elements and complex graphs to your analytical Python code. The best way to get comfortable with Dash is to run through their User Guide and Tutorial.
If you're creating a new Dash app, we recommend starting from our SolveBio Dash Example.
To integrate SolveBio into an existing Dash app you can use our helpful Dash authentication wrapper. First, make sure you have the latest SolveBio Python module and Dash dependencies:
# Install the SolveBio Python module pip install --upgrade solvebio # Install Dash and Dash dependencies pip install --upgrade dash-html-components dash-renderer dash-core-components dash-auth plotly
Next, replace your existing app
object with one created by the SolveBioDash
class instead of the dash.Dash
class. Make sure to plug in the client ID you got by creating your app on SolveBio.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # -*- coding: utf-8 -*- import dash import dash_core_components as dcc import dash_html_components as html # Add the following imports: from solvebio.contrib.dash import SolveBioDash import flask # Before: your app created with the basic Dash class # app = dash.Dash() # After: your app created with SolveBio's Dash class app = SolveBioDash( name=__name__, title='My Dash App', app_url='http://127.0.0.1:8050', client_id='YOUR CLIENT ID HERE') # An example Dash layout: app.layout = html.Div([ html.H1('Welcome to your Dash app!'), html.Div(id='solvebio-auth') ]) @app.callback( dash.dependencies.Output('solvebio-auth', 'children'), [dash.dependencies.Input('solvebio-auth', 'value')]) def solvebio_auth(pathname): """Show current user on first page load.""" user = flask.g.client.User.retrieve() return [ html.P('Logged-in as: {}'.format(user.full_name)), html.A('Log out', href='/_dash-logout') ] if __name__ == '__main__': app.run_server(debug=True) |
In callback functions you can use the flask.g.client
shortcut to access the SolveBio session for the currently logged-in user.
To run the app, open your shell and run the following command:
python app.py
Now, open http://127.0.0.1:8050 in your web browser.
Python Streamlit and SolveBio¶
Streamlit is an open-source Python framework for Machine Learning and Data Science teams. It allows you to create beautiful interactive web apps and dashboards all in pure Python. In just a few minutes you can build a powerful data apps in a couple lines of code. Streamlit dashboard examples can be found in the Streamlit Gallery. The best way to get comfortable with Streamlit framework is to run through their Get started section and the full list of Streamlit building blocks can be found in the API reference section.
If you're creating a new Streamlit app, we recommend starting from our SolveBio Streamlit Example.
To integrate SolveBio into an existing Streamlit app you can use our helpful Streamlit authentication wrapper. First, make sure you have the latest SolveBio Python module and Streamlit dependencies:
# Install the SolveBio Python module pip install --upgrade solvebio # Install Streamlit pip install --upgrade streamlit # Test that the installation worked streamlit hello
SolveBioStreamlit
class is used to wrap Streamlit apps with SolveBio OAuth2. Once the user is successfully authenticated, OAuth2 token
and the initialised SolveClient
are saved to the Streamlit's session state. You can access them:
1 2 | st.session_state.solvebio_client st.session_state.token |
Wrapping minimal Streamlit app with SolveBio OAuth2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import streamlit as st from solvebio_streamlit import SolveBioStreamlit def streamlit_demo_app(): # Getting the solve client from the Streamlit session state solvebio_client = st.session_state.solvebio_client user = solvebio_client.User.retrieve() # Streamlit app contents st.title("SolveBio app") st.header(f"Welcome back {user['first_name']}!") # Wrapping Streamlit app with SolveBio OAuth2 secure_app = SolveBioStreamlit() secure_app.wrap(streamlit_app=streamlit_demo_app) |
To run the app, open your shell and run the following command:
streamlit run app.py
Now, open http://127.0.0.1:8501 in your web browser.
Other Web Frameworks¶
SolveBio currently recommends using Shiny for R and Dash for Python. If you are interested in using other web frameworks such as Django, Rails, React, or other, please contact SolveBio Support.