1-DAV-202 Data Management 2023/24
Previously 2-INF-185 Data Source Integration

Materials · Introduction · Rules · Contact
· Grades from marked homeworks are on the server in file /grades/userid.txt


Difference between revisions of "Ljavascript"

From MAD
Jump to navigation Jump to search
Line 11: Line 11:
 
* You can embed some JavaScript code inside the HTML. This code will be run in browser.
 
* You can embed some JavaScript code inside the HTML. This code will be run in browser.
  
In this case we want JavaScript to visualize data which resides in an SQLite3 database
+
In this case we want JavaScript to visualize data which resides in an SQLite3 database.
 
* Examples in the documentation contain data written directly in the JavaScript code (such as <tt>var data</tt> in the [https://developers.google.com/chart/interactive/docs/gallery/histogram histogram] example).
 
* Examples in the documentation contain data written directly in the JavaScript code (such as <tt>var data</tt> in the [https://developers.google.com/chart/interactive/docs/gallery/histogram histogram] example).
 
* We will also use this simple way, but see the warning below.
 
* We will also use this simple way, but see the warning below.
  
 
So data flow can be as follows:
 
So data flow can be as follows:
* Web browser requests a webpage concerning a user
+
* Web browser requests a webpage concerning a user.
* Your Python code reads data concerning this user from the database and embeds it into the JavaScript portion of a Jinja template
+
* Your Python code reads data concerning this user from the database and embeds it into the JavaScript portion of a Jinja template.
 
** For this you can use jinja for-loop (https://jinja.palletsprojects.com/en/2.11.x/templates/#for). Or you can produce string in Python, which you will put into a HTML. You might need to turn off autoescaping (https://stackoverflow.com/questions/3206344/passing-html-to-template-using-flask-jinja2).  
 
** For this you can use jinja for-loop (https://jinja.palletsprojects.com/en/2.11.x/templates/#for). Or you can produce string in Python, which you will put into a HTML. You might need to turn off autoescaping (https://stackoverflow.com/questions/3206344/passing-html-to-template-using-flask-jinja2).  
 
* The resulting HTML file is sent to the web browser.
 
* The resulting HTML file is sent to the web browser.
Line 24: Line 24:
 
'''Warning:'''
 
'''Warning:'''
 
* Writing data into JavaScript code is a (very) bad practice, but sufficient for this lecture. (A better way is to load data in JSON format through API).
 
* Writing data into JavaScript code is a (very) bad practice, but sufficient for this lecture. (A better way is to load data in JSON format through API).
 
  
 
== Tips ==
 
== Tips ==

Revision as of 21:55, 10 April 2024

HWjavascript

In this lecture we will extend the website from the previous lecture with interactive visualizations written in JavaScript. We will not cover details of the JavaScript programming language, only use visualization contained in the Google Charts library.

Your goal is to take examples from the documentation and tweak them for your purposes.

Overview of the approach

  • Your Python server (in Flask) produces a HTML page with some content.
  • You can embed some JavaScript code inside the HTML. This code will be run in browser.

In this case we want JavaScript to visualize data which resides in an SQLite3 database.

  • Examples in the documentation contain data written directly in the JavaScript code (such as var data in the histogram example).
  • We will also use this simple way, but see the warning below.

So data flow can be as follows:

Warning:

  • Writing data into JavaScript code is a (very) bad practice, but sufficient for this lecture. (A better way is to load data in JSON format through API).

Tips

  • Documentation for each chart contains also HTML+JS code example. That is a good starting point. You can just put it inside the flask template and see what it does.
  • Debugging tip: When looking for errors, limit the amount of data you send through the template, so you can easily read the resulting JS code.
  • Consult the previous lecture on running and accessing Flask applications.

Using multiple charts in the same page (merging examples)

  • You need to include <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> just once. This is similar to import in Python.
  • You can either load individual packages separately by commands of the form: google.charts.load("current", {packages:["calendar"]}); or you can list all packages in one array of the load method.
  • After loading of library is done, you can call function named drawChart as follows: google.charts.setOnLoadCallback(drawChart);. You can either draw multiples charts in one function, or call multiple functions (you need to set multiple callbacks). Be careful to not name multiple functions with same name.
  • Each chart needs its own HTML element with different ID. It looks something like this: <div id="chart_div" style="width: 900px; height: 500px;">. The id chart_div is then referenced here: new google.visualization.Histogram(document.getElementById('chart_div'));
  • Follow the instructions here: [1]