JavaScript is a web-development language used inside HTML pages to add interactive behavior. A beginner can use a few focused snippets without becoming a JavaScript expert. section 13.3.5 section 13.6
In data-science web applications, JavaScript handles browser-side interactions such as:
- Rendering customer churn data as an interactive table with the DataTables library. section 13.3.5
- Listening for a button click, creating a browser download URL, and downloading model predictions as a CSV file. section 13.3.6
Interactive machine-learning app
A Flask page lets users select model features and a label. After predictions are generated, JavaScript exposes a download link and creates a downloadable predictions.csv file when the user clicks it. section 13.3.6
Listing 13.11 Template code to use for rendering an interactive table for the customer churn dataset.
<!DOCTYPE html>
<html>
<head>
<title>Data View</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-3.6.0.min.js"></script> #1
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.js"></script> #1
</head>
<body>
<h1>Customer Data Table</h1>
<table id="customer-table" class="display">
<thead>
<tr>
<th>Total Day Minutes</th> #2
<th>Total Day Calls</th> #2
<th>Total Day Charge</th> #2
<th>Total Night Charge</th> #2
</tr>
</thead>
<tbody>
{% for index, row in customer_data.iterrows() %}
<tr>
<td>{{ row['total_day_minutes'] }}</td>
<td>{{ row['total_day_calls'] }}</td>
<td>{{ row['total_day_charge'] }}</td>
<td>{{ row['total_night_charge'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<script> #3
$(document).ready(function () { #3
$('#customer-table').DataTable(); #3
}); #3
</script> #3
</body>
</html>
Use the template listing to see how JavaScript libraries are imported into HTML and how an interactive customer-data table is assembled. section 13.3.5
Listing 13.14 JavaScript code to download the model predictions file when the download button is clicked
<script>
var predictionsData = {{ csv_data | tojson | safe }};
if (predictionsData) {
var link = document.getElementById('download-predictions');
link.style.display = 'block';
link.addEventListener('click', function () {
var blob = new Blob([predictionsData], { type: 'text/csv' });
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = 'predictions.csv';
a.style.display = 'none';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
});
}
</script>
</body>
</html>
Use the download example to trace JSON data, click-event handling, Blob creation, temporary URLs, and browser-triggered CSV downloads. section 13.3.6

















