JavaScript and Jupyter references

JavaScript is the most important language you need to learn as a frontend developer. Jupyter Notebooks is a convenient way to learn the language without the overhead of creating a full Website. Jupyter Notebooks had ChatGPT plugins to assist with design and troubleshooting problems. This Notebook has colors on HTML pages that were designed with a dark mode background.

output using HTML and CSS

Multiple cells are used to setup HTML in this lesson. Many of the JavaScript cells will use the output tag(s) to write into the HTML that has been setup.

  • %%html is used to setup HTML code block
  • "style" tag enables visuals customization
  • "div" tag is setup to receive data
%%html
<html>
    <head>
        <style>
            #output {
                background-color: #353b45;
                padding: 10px;
                border: 3px solid #ccc;
            }
        </style>
    </head>
    <body>
        <div id="output">
            Hello!
        </div>
    </body>
</html>
Hello!

output explored

There are several ways to ouput the classic introduction message: "Hello, World!"

  • Before you go further, open Console on your Browser. JavaScript developer leaves Console open all the time!!!
  • The function console.log() outputs to Console, this is often used for inspection or debugging.
  • "Hello, World" is a String literal. This is the referred to as Static text, as it does not change. Developer call this a hard coded string.
  • "Hello, World" literal is a parameter to console.log(), element.txt() and alert().
  • The element.txt function is part of Jupyter Notebook %%js magic. This is convenient for Notebook and testing.
  • The alert command outputs the parameter to a dialog box, so you can see it in this Jupyter notebook. The alert commands are shown, but are commented out as the stop run all execution of the notebook.
  • Note, in a Web Application Debugging: An alert is often used for less savy Developers. Console is used by more savy developers; console often requires setting up a lot of outputs. Source level debugging is the most powerful solution for debugging and does not require alert or console commands.
%%js // required to allow cell to be JavaScript enabled
console.log("JavaScript/Jupyter Output Intro");

// Browser Console output; debugging or tracing
console.log("Hello, World!");
console.log("Hello, World Again!");

// Document Object Model (DOM) output; output to HTML, CSS which is standard for a Web Page
// <mark>select element method</mark>: DOM native JavaScript get, document.getElementByID
document.getElementById("output").textContent = "Hello, World!";
// <mark>jQuery CSS-style method</mark>: Tag for DOM selector, $('#output')
$('#output').append('<br><b>Hello World Again!');  // br is break or new line, b is bold

// Jupyter built in magic element for testing and convenience of development
element.text("Hello, World!"); // element is output option as part of %%js magic
element.append('<br><b>Hello World Again!');

//alert("Hello, World!");

multiple outputs using one variable

This second example is a new sequence of code, two or more lines of code forms a sequence. This example defines a variable, thank goodness!!! In the previous example we were typing the string "Hello, World" over and over. Observe with the variable msg="Hello, World!"; we type the string once and now use msg over and over.

  • The variable "var msg =" is used to capture the data
  • The console.log(msg) outputs to console, be sure to Inspect it!
  • The element.text() is part of Jupyter Notebooks and displays as output blow the code on this page. Until we build up some more interesting data for Web Site, we will not use be using the Python HTML, CSS technique.
  • The alert(msg) works the same as previous, but as the other commands uses msg as parameter.
%%js
console.log("Variable Definition");

var msg = "Hello, World!";

// Use msg to output code to Console and Jupyter Notebook
console.log(msg);  //right click browser select Inspect, then select Console to view
element.text(msg);
//alert(msg);

output showing use of a function

This example passes the defined variable "msg" to the newly defined "function logIt(output)".

  • There are multiple steps in this code..
    • The "definition of the function": "function logIt(output) {}" and everything between curly braces is the definitions of the function. Passing a parameter is required when you call this function.
    • The "call to the function:"logIt(msg)" is the call to the function, this actually runs the function. The variable "msg" is used a parameter when calling the logIt function.
  • Showing reuse of function...
    • There are two calls to the logIt function
    • This is called Prodedural Abstraction, a term that means reusing the same code
%%js
console.log("Function Definition");

/* Function: logIt
 * Parameter: output
 * Description: The parameter is "output" to console and jupyter page
*/
function logIt(output) {
    console.log(output); 
    element.append(output + "<br>");
    //alert(output);
}

// First sequence calling logIt function
var msg = "Hello, World!";
logIt(msg);

// Second sequence calling logIt function
var msg = "Hello, <b>Students</b>!" // replaces content of variable
var classOf = "Welcome CS class of 2023-2024."
logIt(msg + "  " + classOf); // concatenation of strings

output showing Loosely typed data

JavaScript is a loosely typed language, meaning you don't have to specify what type of information will be stored in a variable in advance.

  • To define a variable you prefix the name with var or const. The variable type is determined by JavaScript at runtime.
  • Python and many interpretive languages are loosely typed like JavaScript. This is considered programmer friendly.
  • Java which is a compiled language is strongly typed, thus you will see terms like String, Integer, Double, and Object in the source code.
  • In JavaScript, the typeof keyword returns the type of the variable. Become familiar with type as it is valuable in conversation and knowing type help you understand how to modify data. Each variable type will have built in methods to manage content within the data type.
%%js
console.log("Examine Data Types");

// Function to add typeof to output
function getType(output) {
    return typeof output + ": " + output;
}

// Function defintion
function logIt(output) {
    console.log(getType(output));  // logs string
    console.info(output);          // logs object
    element.append(getType(output) + "<br>");  // adds to Jupyter output
    //alert(getType(output));
}

// Common Types
element.append("Common Types <br>");
logIt("Mr M"); // String
logIt(1997);    // Number
logIt(true);    // Boolean
element.append("<br>");

// Object Type, this definition is often called a array or list
element.append("Object Type, array <br>");
var scores = [
    90,
    80, 
    100
];  
logIt(scores);
element.append("<br>");

// Complex Object, this definition is often called hash, map, hashmap, or dictionary
element.append("Object Type, hash or dictionary <br>");
var person = { // key:value pairs seperated by comma
    "name": "Mr M", 
    "role": "Teacher"
}; 
logIt(person);
logIt(JSON.stringify(person));  //method used to convert this object into readable format

Build a Person object and JSON

JavaScript and other languages have special properties and syntax to store and represent data. In fact, a class in JavaScript is a special function.

  • Definition of class allows for a collection of data, the "class Person" allows programmer to retain name, github id, and class of a Person.
  • Instance of a class, the "const teacher = new Person("Mr M", "jm1021", 1977)" makes an object "teacher" which is an object representation of "class Person".
  • Setting and Getting properties After creating teacher and student objects, observe that properties can be changed/muted or extracted/accessed.
%%html
<!-- load jQuery and tablesorter scripts -->
<html>
    <head>
        <!-- load jQuery and DataTables syle and scripts -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
    <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    <style>
            /* CSS-style selector maps to table id or other id's in HTML */
            #jsonTable, #flaskTable {
                background-color: #353b45;
                padding: 10px;
                border: 3px solid #ccc;
                box-shadow: 0.8em 0.4em 0.4em grey;
            }
        </style>
    </head>

    <body>
        <!-- Table for writing and extracting jsonText -->
        <table id="jsonTable">
            <thead>
                <tr>
                    <th>Classroom JSON Data</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td id="jsonText">{"classroom":[{"type":"object","name":"sample","ghID":"sample","classOf":2000,"role":"sample"}]}</td>
                </tr>
            </tbody>
        </table>

    </body>
</html>
Classroom JSON Data
{"classroom":[{"type":"object","name":"sample","ghID":"sample","classOf":2000,"role":"sample"}]}
%%js
console.log("Person objects");

/* class: Person
 * Description: A collection of Person data
*/
class Person {
  /* method: constructor
   * parameters: name, ghID - GitHub ID, classOf - Graduation Class 
   * description: returns object when "new Person()" is called with matching parameters
   * assignment: this.name, this.ghID, ... are properties retained in the returned object
   * default: role uses a default property, it is set to "Student"
  */
  constructor(name, ghID, classOf, role="Student") {
    this.name = name;
    this.ghID = ghID;
    this.classOf = classOf;
    this.role = role;
  }

  /* method: setter
   * parameters: role - role in classroom
   * description: this.role is updated from default value to value contained in role parameter
  */
  setRole(role) {
    this.role = role;
  }
  
  /* method: getter
   * description: turns properties of object into JSON object
   * return value: JSON object
  */
  getJSON() {
    const obj = {type: typeof this, name: this.name, ghID: this.ghID, classOf: this.classOf, role: this.role};
    const json = JSON.stringify(obj);
    return json;
  }

  /* method: logIT
   * description: "this" Person object is logged to console
  */
  logIt() {
    //Person Object
    console.info(this);
    //Log to Jupter
    element.append("Person object in JSON <br>");
    element.append(this.getJSON() + "<br>");  
    //alert(this.getJSON());
  }
    
}

// make a new Person Object
const teacher = new Person("Mr M", "jm1021", 1977); // object type is easy to work with in JavaScript
// update role to Teacher
teacher.setRole("Teacher"); // set the role
teacher.logIt();  // log to console

// make a new Person Object
const student = new Person("Jane Doe", "jane", 2007); // object type is easy to work with in JavaScript
student.logIt(); // log to console

Build a Classroom Array/List of Persons and JSON

Many key elements are shown again. New elements include...

  • Building an Array, "var students" is an array of many persons
  • Building a Classroom, this show forEach iteration through an array and .push adding to an array. These are key concepts in all programming languages.
%%js
// Setting the class Premier with constructors
class Premier {
  constructor(name, team, ranking, goals) {
    this.name = name;
    this.team = team;
    this.ranking = ranking;
    this.goals = goals;
  }
// Setting up the getJSON method with the const jsonString
  getJSON() {
    const jsonString = {
      name: this.name,
      team: this.team,
      ranking: this.ranking,
      goals: this.goals,
     
    };
    const jsonObj = JSON.stringify(jsonString);
    return jsonObj;
  }
// Setting up the logIt method
  logIt() {
    console.info(this);
    element.append(this.getJSON());
  }
}
// Setting up the const Playerdata with all the variables: name, team, ranking, and goals
const Playerdata = {
  name: "Erling Haaland",
  team: "Manchester City",
  ranking: 1,
  goals: 38,
};
/
// Setting up the const js, set as Premier
const js = new Premier(
  Playerdata.name,
  Playerdata.team,
  Playerdata.ranking,
  Playerdata.goals
);

js.logIt();

for loop to generate Table Rows in HTML output

This code extracts JSON text from HTML, that was placed in DOM in an earlier JavaScript cell, then it parses text into a JavaScript object. In addition, there is a for loop that iterates over the extracted object generating formated rows and columns in an HTML table.

  • Table generation is broken into parts...
    • table data is obtained from a classroom array inside of the extracted object.
    • the JavaScript for loop allows the construction of a new row of data for each Person hash object inside of the the Array.
    • in the loop a table row <tr> ... </tr> is created for each Hash object in the Array.
    • in the loop table data, a table column, <td> ... </td> is created for name, ghID, classOf, and role within the Hash object.
%%html
<!-- load jQuery and tablesorter scripts -->
// First Load all of hte jquery and the tablesorter scripts
// The Beginning of the HTML Code, starting off with <head>
<html>
    <head>
        <!-- load jQuery and DataTables syle and scripts -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
    // Link relwith the hptts://cdn.datables
    <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    // Javascript Language
    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    </head>

    <body>
        <table id="jsonTable">
            <thead>
                <tr>
                    <th>Premier League Data/th>
                    // Naming the Table Premier League Data
                </tr>
            </thead>
            <tbody>
                <tr>
                // Putting in the Data of both Erling Haaland and Harry Kane
                    <td id="jsonText">{
                        "player": [
                          {
                            "name": "Erling Haaland",
                            "team": "Manchester City",
                            "ranking": 1,
                          },
                          {
                            "name": "Harry Kane",
                            "team": "Tottenham",
                            "ranking": 4,
                          },
                        ]
                      }
                      </td>
                </tr>
            </tbody>
        </table>

    </body>
</html>
</table> </body> </html> </div> </div> </div> </div> </div>
%%html
# defines the tableID that is going to be referred to in the future
<table id="premierleague">
    <thead>
      <tr>
        # these are the sortTable functions for each of the columns. the onclick allows for on click of the table header. 
        <th onclick="sortTable('name')">Name</th>
        <th onclick="sortTable('team')">Team</th>
        <th onclick="sortTable('goals per game')">Goals Per Game</th>
        <th onclick="sortTable('games played')">Games Played</th>
        <th onclick="sortTable('height')">Height</th>
        <th onclick="sortTable('passes')">Passess</th>
        <th onclick="sortTable('ranking')">Ranking</th>
        <th onclick="sortTable('weight')">Weight</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Erling Haaland</td>
        <td>Manchester City</td>
        <td>2</td>
        <td>35</td>
        <td>88</td>
        <td>276</td>
        <td>1</td>
        <td>210</td>
      </tr>
      <tr>
        <td>Kevin De Bruyne</td>
        <td>Manchester City</td>
        <td>.4</td>
        <td>33</td>
        <td>81</td>
        <td>845</td>
        <td>4</td>
        <td>179</td>
      </tr>
     <tr>
        <td>Bruno Fernandes</td>
        <td>Manchester United</td>
        <td>.3</td>
        <td>35</td>
        <td>88</td>
        <td>764</td>
        <td>15</td>
        <td>167</td>
      </tr>
     <tr>
        <td>Harry Kane</td>
        <td>Tottneham</td>
        <td>.9</td>
        <td>35</td>
        <td>88</td>
        <td>321</td>
        <td>7</td>
        <td>184</td>
      </tr>
    <tr>
        <td>Son</td>
        <td>Tottneham</td>
        <td>.8</td>
        <td>35</td>
        <td>88</td>
        <td>453</td>
        <td>6</td>
        <td>171</td>
      </tr>
    <tr>
        <td>Harry Maguire</td>
        <td>Manchester United</td>
        <td>.1</td>
        <td>35</td>
        <td>88</td>
        <td>567</td>
        <td>27</td>
        <td>195</td>
      </tr>
    <tr>
        <td>Marcus Rashford</td>
        <td>Manchester United</td>
        <td>.6</td>
        <td>35</td>
        <td>88</td>
        <td>345</td>
        <td>31</td>
        <td>182</td>
      </tr>
  </table>
  
  <script>
    // sortTable function meant to sort based on each column header.
    function getColumnIndex(columnName) {
      // each column name is taken in columnName to get its index.
      const table = document.getElementById('premierleague');
      const headerRow = table.getElementsByTagName('thead')[0].getElementsByTagName('tr')[0];
      // contains the column headers, index 0
      const headers = Array.from(headerRow.getElementsByTagName('th'));
      return headers.findIndex(header => header.innerText.toLowerCase() === columnName.toLowerCase()) + 1;
      // converts column header names to lowercase to account for sortTable definitions
    }
    
function sortTable(columnName) {
  const table = document.getElementById('premierleague');
  // constant calls the tableID that was previously defined
  const rows = Array.from(table.tBodies[0].getElementsByTagName('tr'));
  const headerRow = table.getElementsByTagName('thead')[0].getElementsByTagName('tr')[0];
  const isAscending = !headerRow.classList.contains('asc');
    // This code will make sure that the asc class will dec
      // this is a very important line - the asc class helps the function decide whether or not the column is going to be sorted in increasing order or decreasing order.

     rows.sort((rowA, rowB) => {
    let cellA = rowA.querySelector(`td:nth-child(${getColumnIndex(columnName)})`).innerText;
    let cellB = rowB.querySelector(`td:nth-child(${getColumnIndex(columnName)})`).innerText;
    // the nth-child selector is different from normal JS arrays, these have an index starting at 1 rather than 0
    // the rows are sorted based on the column. getColumnIndex is used to get the index of the column. 

    if (columnName.toLowerCase() === 'name' || columnName.toLowerCase() === 'team') {
      return isAscending ? cellA.localeCompare(cellB, undefined, { sensitivity: 'base' }) : cellB.localeCompare(cellA, undefined, { sensitivity: 'base' });
    }
    

    // Convert the cell values to numbers 
      
      cellA = parseInt(cellA);
      cellB = parseInt(cellB);
      
    
    return isAscending ? cellA - cellB : cellB - cellA;
  });
  // The code that follows: Will add the cell values into their sepecified table.
  rows.forEach(row => table.tBodies[0].appendChild(row));
  headerRow.classList.toggle('asc');
}

  </script>
# defines the tableID that is going to be referred to in the future
Premier League Data/th> </tr> </thead>
{ "player": [ { "name": "Erling Haaland", "team": "Los Angeles Lakers", "ranking": 1, }, { "name": "Harry Kane", "team": "Tottenham", "ranking": 4, }, ] }
# these are the sortTable functions for each of the columns. the onclick allows for on click of the table header. </table> </div> </div> </div> </div> </div>
%%html

<html>
<head>
<title>PL Table</title>
<script>
// table element
let table;
// table rows
let rows;
// table header row
let headerRow;
// table headers
let headers;

// onload function fired after body is loaded
// could have used jquery $(document).ready(...);
function doLoad(){
// read the table
table= document.getElementById('premierleague');
// get rows
rows = Array.from(table.tBodies[0].getElementsByTagName('tr'));
// get the header row
headerRow = table.getElementsByTagName('thead')[0].getElementsByTagName('tr')[0];
// get all headers
headers = Array.from(headerRow.getElementsByTagName('th'));
}

// sort the table based on column clicked
// colIndex - column index
// isText - true - column contains textual values
// isText - false - column contains numeric values
function sortTable(colIndex, isText) {
const isAscending = !headerRow.classList.contains('asc');
   
rows.sort((rowA, rowB) => {
// read cell text
   let cellA = rowA.querySelector(`td:nth-child(${colIndex})`).innerText;
   let cellB = rowB.querySelector(`td:nth-child(${colIndex})`).innerText;

   // if the columns are text based, (name and team) then do text compare
   if (isText) {
      return isAscending ? cellA.localeCompare(cellB, undefined, { sensitivity: 'base' }) : cellB.localeCompare(cellA, undefined, { sensitivity: 'base' });
   } else {
    // Convert the cell values to decimal / float numbers
cellA = parseFloat(cellA);
cellB = parseFloat(cellB);
    return isAscending ? cellA - cellB : cellB - cellA;
   }
});
  // set cell values into table
  rows.forEach(row => table.tBodies[0].appendChild(row));
  // set or reset the header row class to asc
  headerRow.classList.toggle('asc');
}
</script>
</head>
<body onload="doLoad()">
<!-- The table -->
<table id="premierleague">
<thead>
<tr>
<!-- set onclick event handlers for each header -->
<th onclick="sortTable(1, true)">Name</th>
<th onclick="sortTable(2, true)">Team</th>
<th onclick="sortTable(3, true)">Goals Per Game</th>
<th onclick="sortTable(4, false)">Games Played</th>
<th onclick="sortTable(5, false)">Height (inches)</th>
<th onclick="sortTable(6, false)">Passess</th>
<th onclick="sortTable(7, false)">Ranking</th>
<th onclick="sortTable(8, false)">Weight</th>
</tr>
</thead>
<tbody>
<tr>
<td>Erling Haaland</td>
<td>Manchester City</td>
<td>2</td>
<td>35</td>
<td>88</td>
<td>5</td>
<td>1</td>
<td>180</td>
</tr>
<tr>
<td>Kevin De Bruyne</td>
<td>Manchester City</td>
<td>.4</td>
<td>33</td>
<td>81</td>
<td>7</td>
<td>4</td>
<td>179</td>
</tr>
<tr>
<td>Bruno Fernandes</td>
<td>Manchester United</td>
<td>.3</td>
<td>35</td>
<td>88</td>
<td>5</td>
<td>15</td>
<td>180</td>
</tr>
<tr>
<td>Harry Kane</td>
<td>Tottneham</td>
<td>.9</td>
<td>35</td>
<td>88</td>
<td>5</td>
<td>7</td>
<td>180</td>
</tr>
<tr>
<td>Son</td>
<td>Tottneham</td>
<td>.8</td>
<td>35</td>
<td>88</td>
<td>5</td>
<td>6</td>
<td>180</td>
</tr>
<tr>
<td>Harry Maguire</td>
<td>Manchester United</td>
<td>.1</td>
<td>35</td>
<td>88</td>
<td>5</td>
<td>27</td>
<td>180</td>
</tr>
<tr>
<td>Marcus Rashford</td>
<td>Manchester United</td>
<td>.6</td>
<td>35</td>
<td>88</td>
<td>5</td>
<td>31</td>
<td>180</td>
</tr>
</table>
</body>
</html>
PL Table
Name Team Goals Per Game Games Played Height Passess Ranking Weight
Erling Haaland Manchester City 2 35 88 276 1 210
Kevin De Bruyne Manchester City .4 33 81 845 4 179
Bruno Fernandes Manchester United .3 35 88 764 15 167
Harry Kane Tottneham .9 35 88 321 7 184
Son Tottneham .8 35 88 453 6 171
Harry Maguire Manchester United .1 35 88 567 27 195
Marcus Rashford Manchester United .6 35 88 345 31 182
</table> </body> </html> </div> </div> </div> </div> </div>
%%html
<!-- load jQuery and tablesorter scripts -->
<!-- this doesn't work in jupyter but will work in GitHub pages -->
<html>
    <head>
        <!-- load jQuery and DataTables syle and scripts -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">
    <!--<script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>-->
    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            $.noConflict();
            console.log("Loading table data");
            $('#premierleague').DataTable({
                // read data from the ajax request
                ajax: 'https://tri3dev.duckdns.org/api/premierleagueplayer/',
                // use follow columns 
                columns: [{"title":"name"},
                    {"title":"team"},
                    {"title":"position"},
                    {"title":"jersey_number"},
                    {"title":"age"},
                    {"title":"height"},
                    {"title":"weight"},
                    {"title":"goals"},
                    {"title":"assists"},
                    {"title":"yellow_cards"},
                    {"title":"red_cards"},
                    {"title":"passes_completed"},
                    {"title":"tackles"},
                    {"title":"clean_sheets"}]
            });
        });
  </script>
    
    </head>

    <body>
<!--defines the tableID that is going to be referred to in the future-->
<table id="premierleague">
<!-- no need to define table header here, DataTable will take care of it -->
</table>

</body>
</html>
Name Team Goals Per Game Games Played Height (inches) Passess Ranking Weight
Erling Haaland Manchester City 2 35 88 5 1 180
Kevin De Bruyne Manchester City .4 33 81 7 4 179
Bruno Fernandes Manchester United .3 35 88 5 15 180
Harry Kane Tottneham .9 35 88 5 7 180
Son Tottneham .8 35 88 5 6 180
Harry Maguire Manchester United .1 35 88 5 27 180
Marcus Rashford Manchester United .6 35 88 5 31 180
What I need to Work on To be a Stronger Developer:
    
1. I need more Creative ideas
2. I need more 

Hacks

One key to these hacks is to build confidence with me going into final grade, I would like to see each student adapt this frontend work in their final project. Second key is the finished work can serve as review for the course, notes for the future in relationship to frontend.

  • Adapt this tutorial to your own work
  • Consider what you need to work on to be stronger developer
  • Show something creative or unique, no cloning
  • Be ready to talk to Teacher for 5 to 10 minutes. Individually!!!
  • Show in Jupyter Notebook during discussion, show Theme and ChatGPT
  • Have a runtime final in GithHub Pages (or Fastpage)
</div>