From cf491ed085c7fb21ab8d94eb7f5c48c17504b525 Mon Sep 17 00:00:00 2001 From: Dylan Lloyd Date: Sat, 13 Jul 2013 22:13:26 -0400 Subject: [PATCH] return json from matrix, be pretty n'ajaxy --- index.php | 42 ++++++++++++++++---------------------- jquery.js => js/jquery.js | 0 js/matrix.js | 43 +++++++++++++++++++++++++++++++++++++++ matrix.py | 29 +++++++------------------- style.css | 43 +++++++++++++++++++++++++++++++++++++++ validate.js | 8 -------- 6 files changed, 111 insertions(+), 54 deletions(-) rename jquery.js => js/jquery.js (100%) create mode 100644 js/matrix.js delete mode 100644 validate.js diff --git a/index.php b/index.php index 199bd3e..d8f84ba 100644 --- a/index.php +++ b/index.php @@ -1,38 +1,32 @@ - + matrixnullspace - - - + + + +
-
-usage: matrix.py
+  

matrix.py

+ Find the kernel, determinant and eigenvalues of a given matrix. -Find the kernel, determinant and eigenvalues of the given matrix. - -example: - -2 3 5 +
+
+ ./calculate +
-
+
    -
    - -

    - -
    +
    + +
    - -
    -

    written by dylan@dylansserver.com -

    - diff --git a/jquery.js b/js/jquery.js similarity index 100% rename from jquery.js rename to js/jquery.js diff --git a/js/matrix.js b/js/matrix.js new file mode 100644 index 0000000..83e8e27 --- /dev/null +++ b/js/matrix.js @@ -0,0 +1,43 @@ +$(document).ready(function(){ + $('#submit').click(function(event){ + if ($('#matrix').val() == '' + || $('#matrix').val().match(/[^.\-\d\s]/) != null) { + event.preventDefault(); + $('#results').fadeOut('slow', function() { + $('#results').html( + $('').attr('id', 'fail').text( + $('#matrix').val() == '' ? + 'Enter a matrix!' : 'Invalid character in matrix.' + )); + $('#results').fadeIn(); + }); + return; + } + $.post('/calculate', { 'matrix' : $('#matrix').val() }) + .done(function(data) { + $('#results').fadeOut('slow', function() { + $('#results').html('').append( + $('
  • ').text('eigenvalues').append( + $('
    ').text(data.eigenvalues)),
    +                    $('
  • ').text('determinant').append( + $('
    ').text(data.determinant)),
    +                    $('
  • ').text('nullspace').append( + $('
    ').text(data.nullspace))
    +                );
    +                $('#results').fadeIn();
    +            });
    +        })
    +        .fail(function(data) {
    +            $('#results').fadeOut('slow', function() {
    +                $('#results').html(
    +                    $('').attr('id', 'fail')
    +                        .text('Uh-oh, something\'s gone wrong!'));
    +                $('#results').fadeIn();
    +            });
    +        });
    +  });
    +  $('textarea').focus(function(e) {
    +      $(e.target).text('');
    +      $(e.target).css({ 'color' : 'black', 'font-style' : 'none' });
    +  });
    +});
    diff --git a/matrix.py b/matrix.py
    index ea6cd9c..5ae2eba 100755
    --- a/matrix.py
    +++ b/matrix.py
    @@ -1,4 +1,5 @@
     import urlparse
    +import json
     
     import scipy
     import scipy.linalg
    @@ -18,10 +19,6 @@ def application(environ, start_response):
         if environ['REQUEST_METHOD'] == 'POST':
             post_data = environ['wsgi.input'].read()
     
    -	output = "
    "
    -	#output += post_data
    -	output += "
    " - post_data = urlparse.parse_qsl(post_data, True, True) data_string = str(post_data[0][1]) @@ -42,25 +39,13 @@ def application(environ, start_response): # output += str(l) determinant = scipy.linalg.det(given_matrix) - output += "

    " - output += "nullspace:
    " - output += str(null_space) - output += "

    " - output += "eigenvalues:
    " - output += str(eigenvalues(given_matrix)) - output += "

    " - output += "determinant:
    " - output += str(determinant) -# output += "

    " -# output += "eigenvalues:
    " -# output += str(l1) -# output += "

    " -# output += "eigenvectors:
    " -# output += str(v[:,0][0]) - output += "

    " + output = json.dumps({ + 'nullspace' : str(null_space), + 'eigenvalues' : str(eigenvalues(given_matrix)), + 'determinant' : str(determinant) + }) - # send results output_len = sum(len(line) for line in output) - start_response('200 OK', [('Content-type', 'text/html'), + start_response('200 OK', [('Content-type', 'application/json'), ('Content-Length', str(output_len))]) return output diff --git a/style.css b/style.css index 6bb5bd9..c9eb003 100644 --- a/style.css +++ b/style.css @@ -23,4 +23,47 @@ body { background-image: url('/images/gray_jean.png'); font-family: 'InconsolataMedium'; + margin: 20px 20px 30px 20px; +} + +#container { + margin: auto; + width: 960px; +} + +form { + float: left; +} + +textarea { + width: 300px; + height: 200px; + margin-bottom: 30px; + color: grey; + font-style: italic; +} + +#results { + float: left; + margin: 0px 0px 0px 20px; +} + +#results li { + font-weight: bold; +} + +#results li :not(child) { + font-weight: normal; +} + +#fail { + color: red; +} + +#clear { + clear: both; +} + +#footer { + width: 100%; } diff --git a/validate.js b/validate.js deleted file mode 100644 index 6f8dd5f..0000000 --- a/validate.js +++ /dev/null @@ -1,8 +0,0 @@ -$(document).ready(function(){ - $('#submit').click(function(event){ - if ( $('#matrix').val() == '' - ){ - event.preventDefault(); - } - }); -}); -- 2.30.2