Skip to content

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 Support is in Beta

SolveBio's Shiny wrapper ("Protected Server") is currently in beta and not recommended for production rollout.

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.

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.

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.