Furthermore, the JS API only provides simple dataset access. For example, it cannot perform SQL queries.
The DSS custom webapps provide a Python backend that can be very useful to process and aggregate dataset in the backend rather than in the browser.
It enables you to access data that would be too large to process in your browser, gives you access to the pandas library to filter and/or aggregate your data, or basically anything you could do in Python.
Then we will go through a slightly more complex example (display top film directors in San Francisco!) .
Python Backend Hello World
Open a new web app and click on the Python tab and click on Enable python backend link.
$.getJSON() to call and retrieve the response of the function
first_call() in the python backend.
$.getJSON calls the Python backend with the url:
The URL is passed as first argument, of the $.getJSON function:
To link the URL to the Python function, we have to add the
app.route decorator before the function declaration:
first_call() return a JSON dictionary that is processed by the function given in second argument of
first_call() returns a JSON dictionary that is interpretable by
This is nice, we requested the backend and printed the answer. But this is not very useful, let's do data processing with the backend.
Let's build our first interactive app with a python backend.
Top film directors
We are going to construct a small application that shows directors and the number of films they realized in San Francisco. We will order directors by the number of films they realized in San Francisco hence showing Film directors that love the most San Francisco!
Retrieve the dataset on San Francisco film locations and upload it in the studio.
Create a new webapp. Then, import the Bootstrap library in your web app:
- First, we load the dataset in the backend, aggregate it by film title (to have one line by film):
- We then aggregate it by director to retrieve the number of films by director. We also sort the result by the number of films.
*Then, we define a function to return our aggregated dataset to the client. We use the handy pandas Dataframe method
to_json() to automatically convert our dataframe to JSON.
In the console, you can see that your Pandas dataframe has been properly return in the form of a json dictionary:
- Now, we would like to display the directors table in the output of our webapp. First we add a container for our table in the HTML part of our editor:
- We use jQuery to add lines to the tables container:
You should know see a classy table in the output of our insight:
We would like to show or hide the table on demand of the user.
- Add two buttons in the HTML:
- Link the click event on the two button to showing or hiding the table with Jquery:
- Congratulations! You have constructed your first interactive app that calls the python backend: