woensdag 11 februari 2015

Getting the data

Before anything can be displayed the data has to come to the user. The most universal way to do so is probably by using JSON. Every front end library supports the JSON format so it will be easy to convert this to any type of chart or display.
Since we want to be able to deploy the server part on both Windows and Linux machines it is best to use a programming language / framework that is natively available on both.
For Windows machines the currently popular route would be to use ASP.NET WebAPI.  It requires the Microsoft stack (.NET, IIS) and is easy to build from Visual Studio. And though it is probably possible to get it running on Linux using Mono that would be much harder.
For a more operating-system agnostic solution it is worth looking at Python and it's myriad of libraries and built-in functions.  Just some Googling for the 'fastest solution to serve JSON from database using Python' brought up several solutions.

The DataSet library does exactly what is needed here. It translates simple calls to the appropriate SQL queries and returns data as JSON objects. A simple abstraction layer removes most direct SQL statements without the necessity for a full ORM model - essentially, databases can be used like a JSON file or NoSQL store.






After setting up the server and my WSGI environment as described, the initial framework of the application is almost ready. The next step would be to retrieve data as generated by the wsgi program from a webpage. The following html page shows the minimal code to do exactly that:

<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
   function loadData(){
      Data = $.ajax({
      url: "get_data",
      dataType: "text",
      success: function(Data) { $("#status_text").text(Data);}
      }
     );
    }
  
  </script>
</head>
<body>
      <p>
         <button onclick="loadData()"> Get data </button>
      </p>
    <p>
     <div id="status_text">WAITING...</div>
   </p>
      
</body></html>





An Ajax function is defined that just gets the data at url 'get_data'. This function is triggered from the onClick handler of the button. The returned values (just interpreted as plain text in this case) are then shown on the page by replacing the WAITING... text.



Geen opmerkingen:

Een reactie posten