<commit_before><!doctype html>
<html>
<head>
<script type="text/javascript" src="static/jquery-1.7.1.min.js"></script>

<script type="text/javascript" src="static/sagews.js"></script>

<script type="text/javascript">
$(function() {
  /*******************************************************
   *     Style 
   ********************************************************/
  $('div').css({'borderWidth':'2px', 'borderStyle':'solid', 
                'padding':'10px', 'height':'150px'})
  $('#input').css({'fontSize':'medium', 'fontFamily':'Courier New'})
 

  /*******************************************************
   * Dynamic behavior
   ********************************************************/

  /* Create an interface to a sagews frontend. */
  var client = sagews.Client('http://localhost:5000'); 

  $('#close_all').click( function() { 
       client.close_all_sessions(function(m) { alert(m.status); })
  })

  $('#new_session').click( function() { 
       client.new_session(function(m) { alert(m.status + '   id=' + m.id); })
  })

  /* TODO: I think this pushing output stuff should get moved to sagews.js.  Then the 
     polling/push/websockets stuff will be automatically taken care of. */
  function insert_output_in_cell(session_id, cell_id, number) {
      $("div").css({'borderColor':'green'});
      client.output_messages(session_id, cell_id, number, function(m) {
	  var i, o, n=m.data.length, done=false;
          if (n === 0) {
	      number -= 1; /* received no new messages */
	  } else {
              for (i = 0; i < n; i++) {
		  o = m.data[i].output;
		  number = m.data[i].number;
		  if (o !== null) {
                      $("#output")[0].innerHTML += o;
		  }
		  if (m.data[i].done) {
		      done = true;
		  }
              }
	  }
          if (done) {
	      $("div").css({'borderColor':'black'})
          } else {
              setTimeout(function() { insert_output_in_cell(session_id, cell_id, number+1); }, 500);
          }
      });
   }


  $('#execute').click( function() { 
       client.execute(0, $('#input').val(), function(m) {
           if (m.status === 'error') {
               $('#output')[0].innerHTML = m.data;
           } else {
               var cell_id = m.cell_id;
               /* clear previous output */
               $('#output')[0].innerHTML = '';
               /* start inserting output in the cell, as it appears: 
                  we do this here *only* because we are not doing polling yet. */
               insert_output_in_cell(0, cell_id, 0);
           }
       });            
  });

  /* Polling: this must completely go away soon! */
  /* Every 1 second, update status of sessions. */
  setInterval(update_session_status, 1000);      
  update_session_status();
})

function update_session_status() {
   var sessions = $.getJSON('http://localhost:5000/sessions', '', 
         function(response, status) { 
              var i, s = '', session;
              for (i=0; i<response['sessions'].length; i++) {
                  session = response['sessions'][i];
                   s += '<br>' + i + ': ' + session.status + ', pid=' + session.pid + ', cells=' + session.next_cell_id;
              }
             $('#sessions')[0].innerHTML = s
         }
   );

   /* $('#sessions').load('http://localhost:5000/sessions') */
}
</script>

</head>

<body>
<title>Demo 1</title>

<div id="runframe">
<textarea id="input" name="code" rows=6 cols=80>
import random, time
for i in range(200):
    print random.randint(0,9),
    if i%50==49: 
        time.sleep(.5); print
</textarea>
</div>

<br>

<button id="execute">Execute</button>
<button id="close_all">Close all</button>
<button id="new_session">New Session</button>


<br><br>

<div>
Output:
<pre id="output">
</pre>
</div>

<br>
Sessions:
<div id="sessions">
loading status...
</div>

</body>


</html>
<commit_msg>finished converting demo to use sagews library.<commit_after><!doctype html>
<html>
<head>
<script type="text/javascript" src="static/jquery-1.7.1.min.js"></script>

<script type="text/javascript" src="static/sagews.js"></script>

<script type="text/javascript">
$(function() {
  /*******************************************************
   *     Style 
   ********************************************************/
  $('div').css({'borderWidth':'2px', 'borderStyle':'solid', 
                'padding':'10px', 'height':'150px'})
  $('#input').css({'fontSize':'medium', 'fontFamily':'Courier New'})
 

  /*******************************************************
   * Dynamic behavior
   ********************************************************/

  /* Create an interface to a sagews frontend. */
  var client = sagews.Client('http://localhost:5000'); 

  $('#close_all').click( function() { 
       client.close_all_sessions(function(m) { });  /* alert(m.status); }) */
  })

  $('#new_session').click( function() { 
       client.new_session(function(m) { }); /* alert(m.status + '   id=' + m.id); }) */
  })

  /* TODO: I think this pushing output stuff should get moved to sagews.js.  Then the 
     polling/push/websockets stuff will be automatically taken care of. */
  function insert_output_in_cell(session_id, cell_id, number) {
      $("div").css({'borderColor':'green'});
      client.output_messages(session_id, cell_id, number, function(m) {
	  var i, o, n=m.data.length, done=false;
          if (n === 0) {
	      number -= 1; /* received no new messages */
	  } else {
              for (i = 0; i < n; i++) {
		  o = m.data[i].output;
		  number = m.data[i].number;
		  if (o !== null) {
                      $("#output")[0].innerHTML += o;
		  }
		  if (m.data[i].done) {
		      done = true;
		  }
              }
	  }
          if (done) {
	      $("div").css({'borderColor':'black'})
          } else {
              setTimeout(function() { insert_output_in_cell(session_id, cell_id, number+1); }, 50);
          }
      });
   }

  $('#sigint').click( function() { client.sigint(0); } )

  $('#sigkill').click( function() { client.sigkill(0); } )

  $('#close_session').click( function() { client.close_session(0); } )

  $('#execute').click( function() { 
       client.execute(0, $('#input').val(), function(m) {
           if (m.status === 'error') {
               $('#output')[0].innerHTML = m.data;
           } else {
               var cell_id = m.cell_id;
               /* clear previous output */
               $('#output')[0].innerHTML = '';
               /* start inserting output in the cell, as it appears: 
                  we do this here *only* because we are not doing polling yet. */
               insert_output_in_cell(0, cell_id, 0);
           }
       });            
  });

  /* Polling: this must completely go away soon! */
  /* Every 1 second, update status of sessions. */
  setInterval(update_session_status, 1000);      
  update_session_status();

  function update_session_status() {
      client.sessions(function (m) {
          var i, s = '', session;
          for (i = 0; i < m.sessions.length; i++) {
              session = m.sessions[i];
              s += '<br>' + i + ': ' + session.status + ', pid=' + session.pid + ', cells=' + session.next_cell_id;
          }
          $('#sessions')[0].innerHTML = s
      })
  }

})


</script>

</head>

<body>
<title>Demo 1</title>

<div id="runframe">
<textarea id="input" name="code" rows=6 cols=80>
import random, time
for i in range(200):
    print random.randint(0,9),
    if i%50==49: 
        time.sleep(.5); print
</textarea>
</div>

<br>

<button id="execute">Execute</button>
<button id="sigint">Interrupt</button>
<button id="sigkill">Kill</button>
<button id="close_session">Close</button>
<button id="close_all">Close all</button>
<button id="new_session">New Session</button>


<br><br>

<div>
Output:
<pre id="output">
</pre>
</div>

<br>
Sessions:
<div id="sessions">
loading status...
</div>

</body>


</html>

{'fraction_non_alphanumeric': 0.23449587824984147, 'fraction_numerical': 0.013189600507292327, 'mean_word_length': 2.6290842153704554, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6137627', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2431, 'n_words': 531}