CSV Import and Export Functionality in DataViewsJS
DataViewsJS is a powerful JavaScript data presentation and datagrid platform for creating complex and visually interesting data views. In some cases, importing CSV files (or other data) and exporting CSV files is needed for universally sharing the data.
Here, we'll go over how to create a simple application that imports data from a CSV file and directly from a data source, as well as how to export that data to a CSV file.
Project Setup
Before writing any code, we will first need to import all the libraries we will use in the application. We need to navigate to the project's folder in a terminal and enter the following command:
We can then add references to these files and a script file to hold all of the script code we will write in an HTML file:
We can also get the page content setup; in this case, we want to have three buttons:
- Import a CSV file
- Import data from a data source
- Export DataViewsJS to a CSV file
We should also add the DIV element for the DataViewsJS instance:
Setup Script Code
In this sample, we are going to be using a free stock API known as Alpha Vantage. Specifically, we are going to export historical market data from Alpha Vantage and manipulate the data in DataViewsJS. You may also refer to this article that covers some of the other stock API options and financial modeling best practices.
In order to access the data from that API, you will need an API key.
We are also going to be using a GitHub repo that is essentially a JavaScript wrapper for the Alpha Vantage API.
We can initialize that interface with our key:
Import CSV Files
In the case of this application, we are importing a CSV file into DataViewsJS that was downloaded from Alpha Vantage.
A link to that file can be found here.
You just need to replace <Your Key Here>
with the Alpha Vantage API key. For simplicity, I have already included the CSV file in the demo zip file.
For this functionality, we want to hide the buttons for import/export, import the CSV file in our application, convert the CSV to an array of JavaScript objects, and set that array as a data source for DataViewsJS.
We can accomplish this with the jQuery AJAX call:
The CSVToJSObject function parses through the CSV content and creates an array of JavaScript Objects:
When we have the array, we initialize the DataViewsJS instance with that:
Import Data Source and Export to CSV
The other way we can import the data is directly from a data source, and this is where the custom Alpha Vantage wrapper comes into play: we can call the API to get the data and then write a custom function to parse through that data and make it more like the CSV file we have:
Comparing the data to the CSV from before shows that the JavaScript object array is slightly different, so we need to have a function that parses through that data to make it fit our requirements:
Once that data has been loaded into DataViewsJS, we can write a function to export the DataViewsJS instance to a CSV file:
That is all that is needed to import and export CSV files with DataViewsJS!
Check out the latest release of DataViewsJS here and our DataViewsJS Documentation.