How to Share Flask APIs with Shiny as Applications

[This article was first published on R – Predictive Hacks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

As a Data Scientist, you may work in both R and Python and it is common to prefer one language over the other for some specific tasks. For example, you may prefer R for Statistics, Data Cleansing and Data Visualizations and you may prefer Python for NLP tasks and Deep Learning. Also, when it comes to Restful APIs, Python Flask APIs have an advantage over R Plumber and Restrserve.

The Scenario

Assume that you have built a model in Python and on top of that, you have built a Flask API. Regarding the UI, you prefer to work with Shiny. So, the scenario is that you want to share your Python Flask API using R Shiny. Let’s see how we can do it with a hands-on example.

The Flask API

We have provided an example of a Flask API for Sentiment Analysis. For convenience, we provide the code below:

The requirements.txt file is the following:


The file:

from flask import Flask, request, jsonify
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
application = Flask(__name__)
def get_sentiment(my_text):
    vs = analyzer.polarity_scores(my_text)
    sentiment = ''
    if vs['compound'] >= 0.05:
        sentiment = 'Positive'
    elif vs['compound'] <= -0.05:
        sentiment = 'Negative'
        sentiment = 'Neutral'
@application.route("/endpoint", methods=['GET','POST'])
def sentiment_endpoint():
    if request.method == 'POST':
        json_dict = request.get_json()
        if 'my_text' in json_dict:
            result = get_sentiment(json_dict['my_text'])
            return jsonify({'output' : result})
            return jsonify({
                "status": "failed",
                "message": "parameter 'my_text' is required!"
    if request.method == 'GET':
        my_text = request.args.get('my_text')
        result = get_sentiment(my_text)
        return jsonify({'output' : result})
if __name__=='__main__':

Call an API with R

We will provide an example of how you can call the above Flask API with R. For this example, we run the API locally, that is why the URL is and the sentiment analysis, the route is the endpoint that is why we will call the URL.

Let’s get the sentiment of the sentence:

What a great experience! I feel really happy ????



body<-list(my_text="What a great experience! I feel really happy :)")

b<-POST(url, body = body, encode = "json")

t1<-content(b, type="application/json")



[1] "Positive"

As expected, the sentiment of the sentence was positive.

Build a Shiny Application

We have provided an example of How to Share your Machine Learning Models with Shiny. It would be helpful to have a look at it since we will apply the same architecture.

The Shiny Application will have two functionalities

  • To get a phrase as input and to return the sentiment such as “positive“, “neutral“, “negative“.
  • To has an option to upload a .txt file, tab-separated with many phrases and to return a .txt file by adding a column of the sentiments.

Let’s build the Shiny App:


# Define UI for application that draws a histogram
ui <- fluidPage(
    # Application title
    titlePanel("Sentiment Analysis"),
    # Sidebar with a slider input for number of bins 
            textInput("caption", label="Enter your text here.", value="", placeholder = "Phrase to get a Sentiment..."),
            # Input: Select a file ----
            fileInput("file1", "upload csv file here",
                      multiple = FALSE,
                      accept = c("text/csv",
            # Button
            downloadButton("downloadData", "Download the Predictions")
        # Show the table with the predictions

# Define server logic required to draw a histogram
server <- function(input, output) {
        df <- read.csv(input$file1$datapath, sep="\t", stringsAsFactors = FALSE)
        for (i in 1:nrow(df)) {
            tmpdf<-data.frame(InputText=df[i,1], Sentiment=t1$output)
            fdf<-rbind(fdf, tmpdf)                   
    output$mytable <- DT::renderDataTable({
        return(DT::datatable(reactiveDF(),  options = list(pageLength = 100), filter = c("top")))
    # Downloadable csv of selected dataset ----
    output$downloadData <- downloadHandler(
        filename = function() {
        content = function(file) {
            write.csv(reactiveDF(), file, row.names = FALSE)

# Run the application 
shinyApp(ui = ui, server = server)

Get the Sentiments

Let’s see how the Shiny App works.

Get the Sentiment of a document in an interactive way

Let give as input the following input:

kudos! Great job!

As we can see, the Shiny App give us the chance to give the input in and it returns the sentiment in an interactive way

How to Share Flask APIs with Shiny as Applications 1

Get the Sentiment of multiple documents in an interactive way

In case we have many documents, like thousands of reviews, and we want to get the sentiment of each one, the Shiny App gives us the option to upload the data and it also to download them with an extra column of the sentiment score.

The input file

How to Share Flask APIs with Shiny as Applications 2

Let’s upload it to Shiny and get the results:

How to Share Flask APIs with Shiny as Applications 3

As we can see, it accepted as input the txt file and it returned the input text with the predicted sentiments. We can also download the file which is the following:

How to Share Flask APIs with Shiny as Applications 4

The Takeaway

The takeaway is that you can work with Python and Flask APIs for the backend part and to work with the Shiny for the front end to share your models as applications. So, let’s keep in mind that Shiny can be an alternative to Flask jinja.

To leave a comment for the author, please follow the link and comment on their blog: R – Predictive Hacks. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)