Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>

<head>
  <title>Model</title>
  <script src="/assets/main.js"></script>
  <link href="/assets/styles.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:300,400|Saira+Condensed&display=swap" rel="stylesheet">
</head>

<body>
  <div id="outer-frame">
    <div id="inner-frame">
      <div id="header">
        <div id="title">
          <span>Iris Classifier</span><br>
          <div style="margin-top: -15px; font-size: 16px; font-family: 'Saira Condensed', sans-serif; color: #a67dbe;"><i>by</i> Al-Ahmadgaid B. Asaad</div>
        </div>
        <div id="server-controls">
          <div class="segmented-control">
            Select Server&nbsp;&nbsp;&nbsp;&nbsp;
            <div class="segmented-control-item">
              <input class="segmented-control-input" type="radio" value="1" name="option" id="option-julia" checked>
              <label id="julia-server" class="segmented-control-label" for="option-julia">Julia</label>
            </div>
            <div class="segmented-control-item">
              <input class="segmented-control-input" type="radio" value="2" name="option" id="option-python">
              <label id="python-server" class="segmented-control-label" for="option-python">Python</label>
            </div>
          </div>
        </div>
      </div>
      <table id="content">
        <tbody>
          <tr>
            <td id="app-desc">
              A web application for classifying Iris species based on its sepal and petal morphology.
              <div style="display: block; height: 72px;"></div>
              <table id="specs" style="vertical-align: bottom;">
                <tbody>
                  <tr>
                    <!--<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>-->
                    <td class="key">Model</td>
                    <td>MultiLayer Perceptron</td>
                  </tr>
                  <tr>
                    <td class="key">DL Platform</td>
                    <td>Knet.jl and TF-Keras</td>
                  </tr>
                  <tr>
                    <td class="key">Test Accuracy</td>
                    <td id="test-accuracy">0% (Knet.jl); 0% (TF-Keras)</td>
                  </tr>
                </tbody>
              </table>
            </td>
            <td>
              <input class="iris-inputs" id="sepal-length" type="text" placeholder="Sepal Length">
              <input class="iris-inputs" id="sepal-width" type="text" placeholder="Sepal Width">
              <input class="iris-inputs" id="petal-length" type="text" placeholder="Petal Length">
              <input class="iris-inputs" id="petal-width" type="text" placeholder="Petal Width">
            </td>
          </tr>
        </tbody>
      </table>
      <div id="footer">
        <div id="button-wrappers">
          <button id="train" type="button">Train</button>
          <button id="classify" type="button">Classify</button>
        </div>
        <div id="output">
        </div>
      </div>
    </div>
    <div id="status">
      <div class="lds-ellipsis">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
    </div>
  </div>
</body>

</html>
              
            
!

CSS

              
                body {
  height: 100%;
  width: 100%;
  background-color: #f8d382;
}

div#outer-frame {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;

  margin: auto auto;
  width: 600px;
  height: 377px;
  padding: 0 0 20px 0;
}

div#inner-frame {
  text-align: center;
  padding: 20px 40px 30px 40px;
  text-align: center;
  border-width: 0px;
  border-radius: 10px;
  box-shadow: 0px 20px 50px 3px rgba(160, 117, 0, 0.5);
  background-image: linear-gradient(#30034b, #230435);
}

div#header {
  text-align: left;
  display: block;
  width: 100%;
  margin: 0;
}

div#server-controls {
  text-align: right;
  float: right;
  width: 60%;
}

div#title {
  display: inline-block;
  font-family: "Saira Condensed", sans-serif;
  font-size: 40px;
  text-align: left;
  color: #bea1cf;
  width: 38%;
}

table#content {
  padding: 20px 0;
}

td {
  vertical-align: top;
}

td#app-desc {
  text-align: left;
  padding: 5px 10px 0 0;
  font-size: 14px;
  font-family: "Roboto", sans-serif;
  color: #a67dbe;
}

input {
  display: block;
  font-family: "Saira Condensed", sans-serif;
  font-size: 16px;
  margin: 8px 0px 8px 20px;
  padding: 5px;
  width: 120px;
  height: 27px;
  border-width: 0px;
  border-radius: 5px;
  text-align: center;
  box-shadow: inset 0 0 5px 1px rgba(110, 110, 110, 0.5);
  background-color: #ecd4fa;
  color: #30034b;
  transition: all 0.3s ease-in-out;
}

::placeholder {
  color: #ad8ebe;
}

input:active {
  background-color: #eeeeee;
}

input:hover {
  background-color: #fcf6e9;
}

div#footer {
  text-align: left;
}

div#button-wrappers {
  margin-right: -3px;
  text-align: left;
  display: inline-block;
  width: 50%;
}

div#output {
  text-align: center;
  vertical-align: middle;
  width: 25%;
  margin-left: 125px;
  font-family: "Saira Condensed", sans-serif;
  font-size: 23px;
  display: inline-block;
  transition: all 0.3s ease-in-out;
  color: #bea1cf;
}

button {
  font-size: 16px;
  font-family: "Roboto", sans-serif;
  border-radius: 5px;
  border-width: 0;
  padding: 10px;
  margin-right: 3px;
  background-color: rgb(223, 107, 12);
  color: #ffffff;
  transition: all 0.3s ease-in-out;
  box-shadow: 0px 0px 2px 1px rgb(23, 0, 27);
}

button:hover {
  background-color: rgb(255, 187, 0);
  color: #30034b;
}

button:active {
  background-color: rgb(223, 107, 12);
  color: #30034b;
}

table#specs {
  border-collapse: collapse;
  width: 80%;
  font-size: 13px;
  font-family: "Saira Condensed", sans-serif;
}

table#specs tbody tr td.key {
  background-color: #eedcf833;
}

table#specs tbody tr td {
  padding-left: 5px;
  border: 1px solid #723691;
}

table#specs tbody tr td#test-accuracy {
  background-color: init;
  color: init;
}

.updated-accuracy {
  -webkit-animation: emphasize 3s linear;
  /* Safari 4.0 - 8.0 */
  animation: emphasize 3s linear;
}

/* Safari 4.0 - 8.0 */
@-webkit-keyframes emphasize {
  from {
    background-color: init;
    color: init;
  }

  to {
    background-color: #eedcf833;
    color: rgb(255, 255, 255);
  }
}

@keyframes emphasize {
  from {
    background-color: init;
    color: init;
  }

  to {
    background-color: #eedcf833;
    color: rgb(255, 255, 255);
  }
}

/* Copied from */
.segmented-control {
  display: table;
  float: right;
  width: 60%;
  margin: 20px 4px 0 0;
  padding: 0;
  font-family: "Roboto", sans-serif;
  font-size: 13px;
  color: #a67dbe;
}

.segmented-control-item {
  display: table-cell;
  margin: 0;
  padding: 0;
  list-style-type: none;
  text-align: right;
}

.segmented-control-input {
  padding: 0;
  margin: 0;
  position: absolute;
  visibility: hidden;
}

.segmented-control-label {
  display: block;
  margin: 0 -1px -1px 0;
  /* -1px margin removes double-thickness borders between items */
  padding: 3px 6px;

  border: 1px solid #eedcf833;
  font-family: "Roboto", sans-serif;
  font-size: 13px;
  text-align: center;
  color: #eedcf833;
  cursor: pointer;
  transition: all 0.3s ease-in-out;
}

#julia-server.segmented-control-label {
  border-radius: 4px 0 0 4px;
}

#python-server.segmented-control-label {
  border-radius: 0 4px 4px 0;
}

.segmented-control-label:hover {
  color: #30034b;
  background-color: rgb(255, 187, 0);
}

.segmented-control-input:checked + .segmented-control-label {
  background: rgb(223, 107, 12);
  color: #ffffff;
  box-shadow: inset 0px 0px 3px 1px rgb(182, 83, 2);
}

div#status {
  background-color: rgb(138, 64, 16);
  color: #ffffff;
  width: 80%;
  margin: 0 auto;
  border-radius: 0 0 10px 10px;
  padding: 0;
  text-align: center;
  font-family: "Roboto", sans-serif;
  font-size: 11px;
  box-shadow: inset 0px 2px 5px 1px rgb(71, 32, 0);
  overflow: hidden;
  /* transition: all 200ms ease-out; */
  transition-property: all;
  transition-duration: 200ms;
  transition-timing-function: cubic-bezier(0, 0, 0.58, 1);
  transform: translateZ(0);
  height: 0;
}

div#status.hidden {
  padding: 7px;
  height: 13px;
}

/* loading animation from https://loading.io/css/ */
.lds-ellipsis {
  display: inline-block;
  position: relative;
  width: 35px;
  height: 10px;
}

.lds-ellipsis div {
  position: absolute;
  top: 4px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: #fff;
  animation-timing-function: cubic-bezier(0, 1, 1, 0);
}

.lds-ellipsis div:nth-child(1) {
  left: 1px;
  animation: lds-ellipsis1 0.6s infinite;
}

.lds-ellipsis div:nth-child(2) {
  left: 1px;
  /* background-color: red; */
  animation: lds-ellipsis2 0.6s infinite;
}

.lds-ellipsis div:nth-child(3) {
  left: 10px;
  /* background-color: yellow; */
  animation: lds-ellipsis2 0.6s infinite;
}

.lds-ellipsis div:nth-child(4) {
  left: 20px;
  /* background-color: greenyellow; */
  animation: lds-ellipsis3 0.6s infinite;
}

@keyframes lds-ellipsis1 {
  0% {
    transform: scale(0);
  }

  100% {
    transform: scale(1);
  }
}

@keyframes lds-ellipsis3 {
  0% {
    transform: scale(1);
  }

  100% {
    transform: scale(0);
  }
}

@keyframes lds-ellipsis2 {
  0% {
    transform: translate(0, 0);
  }

  100% {
    transform: translate(10px, 0);
  }
}
              
            
!

JS

              
                window.onload = function() {
  addListener();
};

function httpRequest(URL, data, callback) {
  let xhr = new XMLHttpRequest();

  xhr.open("POST", URL, true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.send(JSON.stringify(data));
  xhr.onreadystatechange = function() {
    if (this.readyState === 4 && this.status === 200) {
      let output = JSON.parse(this.responseText);
      callback(output);
    } else if (this.readyState === 4 && this.status !== 200) {
      callback(false);
    } else {
      console.log(
        "readyState is " + this.readyState + " and status is " + this.status
      );
    }
  };
}

function addListener() {
  document.getElementById("classify").addEventListener("click", classify);
  document.getElementById("train").addEventListener("click", train);
  document.getElementById("option-julia").addEventListener("change", setJulia);
  document
    .getElementById("option-python")
    .addEventListener("change", setPython);
}

function setJulia() {
  let jlServer = document.getElementById("option-julia");
  if (jlServer.checked) {
    status("Changed back-end server to Julia.", 2000);
  }
}

function setPython() {
  let pyServer = document.getElementById("option-python");
  if (pyServer.checked) {
    status("Changed back-end server to Python.", 2000);
  }
}

function classify() {
  let irisInputs = document.getElementsByClassName("iris-inputs");

  let data = [];
  let nanCounter = 0;
  let strCounter = 0;
  for (let i = 0; i < irisInputs.length; ++i) {
    if (isNaN(parseFloat(irisInputs[i].value)) && irisInputs[i].value !== "") {
      strCounter += 1;
    } else {
      data.push(parseFloat(irisInputs[i].value));
      if (isNaN(data[i])) {
        nanCounter += 1;
      }
    }
  }
  let accy = document.getElementById("test-accuracy");
  let jlServer = document.getElementById("option-julia");

  if (jlServer.checked) {
    var url = "http://0.0.0.0:8081/classify";
  } else {
    var url = "http://0.0.0.0:8082/classify";
  }

  if (jlServer.checked && accy.innerHTML[0] === "0") {
    status("Knet.jl test accuracy is 0%, please train the model first.", 3000);
  } else if (
    !jlServer.checked &&
    accy.innerHTML[accy.innerHTML.lastIndexOf(";") + 2] === "0"
  ) {
    status("TF-Keras test accuracy is 0%, please train the model first.", 3000);
  } else if (strCounter > 0) {
    status("Please enter integer/float values.", 3000);
  } else if (nanCounter > 0) {
    status("Please do not leave a blank field.", 3000);
  } else {
    status(
      "Sending request to the server at http://0.0.0.0:8081/classify",
      0,
      true
    );
    httpRequest(url, { data: data }, function(output) {
      if (output === false) {
        status("", 0);
        status(
          "Status: 0 (no response from the server), returning random prediction.",
          3000
        );
        let items = [
          "&#128144; Setosa",
          "&#127799; Versicolor",
          "&#127802; Virginica"
        ];
        let item = items[Math.floor(Math.random() * items.length)];
        document.getElementById("output").innerHTML = item;
      } else {
        let jlServer = document.getElementById("option-julia");
        if (jlServer.checked) {
          status("", 0);
          status(
            "Status: 200 (OK), processing prediction from Knet.jl model.",
            2500
          );
          document.getElementById("output").innerHTML = output["data"];
        } else {
          status("", 0);
          status(
            "Status: 200 (OK), processing prediction from TensorFlow-Keras model.",
            2500
          );
          document.getElementById("output").innerHTML = JSON.parse(output)[
            "data"
          ];
        }
      }
    });
  }
}

function train() {
  let jlServer = document.getElementById("option-julia");

  if (jlServer.checked) {
    var url = "http://0.0.0.0:8081/train";
  } else {
    var url = "http://0.0.0.0:8082/train";
  }

  status("The model is now training.", 0, true);
  httpRequest(url, { data: "train" }, function(output) {
    if (output === false) {
      updateAccuracy(output);
    } else {
      updateAccuracy(output);
    }
  });
}

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

function accuracyLab() {
  let jlServer = document.getElementById("option-julia");
  let accy = document.getElementById("test-accuracy");

  if (jlServer.checked) {
    if (accy.innerHTML[2] !== "%" && accy.innerHTML[2] !== " ") {
      var labs = accy.innerHTML.slice(3);
    } else if (accy.innerHTML[1] !== "%") {
      var labs = accy.innerHTML.slice(2);
    } else {
      var labs = accy.innerHTML.slice(1);
    }
  } else {
    var refIdx1 = accy.innerHTML.indexOf(";");
    var refIdx2 = accy.innerHTML.indexOf("(TF");
    var labs1 = accy.innerHTML.slice(0, refIdx1);
    var labs2 = accy.innerHTML.slice(refIdx2 - 2);
    var labs = [labs1, labs2];
  }

  return [accy, labs];
}

function updateAccuracy(output) {
  let alab = accuracyLab();
  let accy = alab[0];
  let labs = alab[1];
  // let jlServer = document.getElementById("option-julia");

  if (output === false) {
    status("", 0);
    status(
      "The model is now training (mock-up because server is down).",
      5000,
      true,
      () => {
        if (typeof labs === "string") {
          accy.innerHTML = getRndInteger(90, 100) + labs;
          status("Done! Updated Knet.jl test accuracy above.", 3000);
        } else {
          accy.innerHTML = labs[0] + "; " + getRndInteger(90, 100) + labs[1];
          status("Done! Updated TF-Keras test accuracy above.", 3000);
        }
        emphasize();
      }
    );
  } else {
    status("", 0);
    if (typeof labs === "string") {
      accy.innerHTML = Math.round(output["data"] * 100) + labs;
      status("Done! Updated Knet.jl test accuracy above.", 3000);
    } else {
      accy.innerHTML =
        labs[0] +
        "; " +
        Math.round(parseFloat(JSON.parse(output)["data"]) * 100) +
        labs[1];
      status("Done! Updated TF-Keras test accuracy above.", 3000);
    }
    emphasize();
  }
}

function emphasize() {
  let accuracyDiv = document.getElementById("test-accuracy");
  accuracyDiv.classList.toggle("updated-accuracy");

  setTimeout(() => accuracyDiv.classList.toggle("updated-accuracy"), 3000);
}

function status(text, timeout, showLoading = false, callback = false) {
  let statusDiv = document.getElementById("status");

  if (showLoading) {
    statusDiv.innerHTML =
      '<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>' +
      text;
  } else {
    statusDiv.innerHTML = text;
  }
  if (timeout < 1) {
    statusDiv.classList.toggle("hidden");
  } else {
    statusDiv.classList.toggle("hidden");
    setTimeout(() => {
      if (callback === false) {
        statusDiv.classList.toggle("hidden");
      } else {
        callback();
        statusDiv.classList.toggle("hidden");
      }
    }, timeout);
  }
}

              
            
!
999px

Console