Webapps: Difference between revisions
From charlesreid1
| Line 62: | Line 62: | ||
If we are drawing data from a large database on the server side, the data that is initially sent is just a small, initial bundle of data - like the first place you see when you open Google Maps. As the client interacts with the map, and requests different information, that Javascript code will ask the server for additional data. | If we are drawing data from a large database on the server side, the data that is initially sent is just a small, initial bundle of data - like the first place you see when you open Google Maps. As the client interacts with the map, and requests different information, that Javascript code will ask the server for additional data. | ||
=Protocols= | |||
When two computers talk to each other, there is a thick sandwich of protocols that make that communication possible. These include multiple network protocols and web protocols. And sitting right on top of that thick sandwich is the HTTP protocol, which is the primary protocol used by the internet. | |||
First, what kind of things are the client and server communicating? The client communicates things like "Can I have charlesreid1.com?" and "Can I have this piece of data from the database?" The server will communicate things like, "Here are all the files required for charlesreid1.com," or "Here is the piece of information you requested from the database." | |||
There are two kinds of requests that a browser will send a request to an HTTP web server: a GET request, and a POST request. | |||
==GET== | |||
Think of GET requests as a very simple set of key-value pairs. These key-value pairs are actually encoded directly into the URL, so if I request a page like: | |||
<pre> | |||
http://charlesreid1.com/page.html | |||
</pre> | |||
then i can send along a key-value pair dictionary by adding them to the URL that I type in my browser: | |||
<pre> | |||
http://charlesreid1.com/page.html?variable1=bananas&variable2=apples | |||
</pre> | |||
Now the code on <code>page.html</code> can use these GET variables. | |||
GET variables are used to encode simple information, like "Look at page 5," in the URL. But as the amount of information in a request gets more complex, GET doesn't work so well. | |||
==POST== | |||
The GET method obviously isn't scalable for anything but simple information. For everything else, there's POST. POST requests are sent to the server silently, behind the scenes. This allows POST to be used to transfer large amounts of data. | |||
Combined with a format for serializing data (i.e., turning a chunk of data into a stream), POST can be used to send arbitrary data between a server and a client. | |||
==POST JSON== | |||
Enter JSON - Javascript Serial Object Notation. This is a way of taking Javascript objects (a dictionary for all you Python people out there, a Javascript object is a Python dictionary) and serializing them. A simple example of JSON looks like this: | |||
<pre> | |||
{ | |||
"variable1" : "bananas", | |||
"variable2" : "apples" | |||
} | |||
</pre> | |||
These can also be nested, and include arrays, or lists, of values: | |||
<pre> | |||
{ | |||
"variable1" : { | |||
"fruit" : "bananas", | |||
"possible attacks" : ["killer bee banana attack","hidden banana peel death-squeeze"] | |||
}, | |||
"variable2" : { | |||
"fruit" : "apples", | |||
"possible attacks" : ["the choker","death from above"] | |||
} | |||
} | |||
</pre> | |||
[[Category:Javascript]] | [[Category:Javascript]] | ||
[[Category:Python]] | [[Category:Python]] | ||
Revision as of 22:34, 27 July 2015
I've created web apps using a couple of different languages.
My first introduction to web apps was through PHP, a dynamic, server-side web programming language. I use PHP for running web apps that other people have written, like MediaWiki and Wordpress. I occasionally use it for my website.
The way I usually develop web apps is using a combination of Python and Javascript, or just plain Javascript. If the web app is doing something complicated, I'll use Python; otherwise, I can usually get the job done in Javascript.
Below are some basic concepts about web apps that'll be useful to know.
Servers and Clients
Server-Side vs Client-Side
The biggest decision to make, when designing a web app, is deciding what tasks will be run by the client, and what tasks (if any) will be run by the server.
Let me explain.
Your typical webapp consists of two parties: the client, the end user of the webapp, and the server, the provider of the webapp.
Most typically, you're using the HTTP protocol - that's why it's a WEB app. Using that HTTP protocol, you can send requests and information back and forth between the server and client. HTTP is a COMMON protocol, meaning you can turn data and variables in Python or Javascript into HTTP information, and vice-versa. This allows the client and server, running the two sides of the webapp, to communicate.
Deciding on whether to run parts of a web app on the client or on the server can have big implications for the complexity and speed of your webapp. If code runs only on the client side, it will be very fast, and all your server has to do is send some files, which means your server won't get bogged down as more users try and use the webapp.
But client-side-only web apps are pretty boring (unless you're into playing games). Much more interesting is the case of server-side applications. These might involve looking up data in a database or performing calculations. It might involve grabbing information from a sensor, or sending information to and from a microcontroller. There are many more interesting remote applications (at least, in my mind).
Python vs Javscript
We've seen that the main concern when designing a web app is location: client or remote side. This also has implications for the languages that we can use.
Javascript is designed as a client-side language, since it runs in the browser (although it can be hacked so you can use it as a server side language). Python, on the other hand, does not run in the browser, so it lacks the portability of Javascript. But Python has enormous power and an amazing selection of libraries, making it a great do-anything language:
A Simple Example
Let's make all this abstract stuff a little more concrete with a simple example.
Open this webapp in a new window: http://charlesreid1.github.io/a-shrubbery/educationca/
You'll see something like this:
This is a simple client-side web app that visualizes map-related data. It is hosted on GitHub pages, which means that the server, in this case, is a server at GitHub's data center. The client is you - or, more specifically, your browser.
When you open a regular HTML website in your browser, your browser sends a request for that page to the server (the protocol for that request is HTTP). The server sees the request, and returns the HTML for that page to your browser, which then renders it and shows it to you.
When you open a web app that has client-side Javascript in your browser, your browser sends a request to the server, as before. The server returns the HTML for that page, plus the Javascript code to execute. That HTML is rendered, and that Javascript is executed, and the end result is a client-side, dynamic website.
When we request a Javascript web app, the process looks exactly the same, except now the returned page includes a bundle of Javascript code, which the browser will execute in addition to displaying the HTML.
Let's take a look at what the server is sending the client.
Here, we take a peek into what the server is sending as a response, and we can see HTML and Javascript files. These are executed by the browser to create the web app for the user.
The main HTML page contains the necessary material for a webpage: the white background, the header, the navigation bar, etc. The dynamic web app in our Javascript file provides the code for the web app. It references several other files, such as a file with Javascript code for drawing data on a map, and a file with Javascript code for drawing charts displaying map data. These, in turn, refer to other Javascript files, or to data files. The server sends along everything that the web app specifies is needed for the initial request.
If we are drawing data from a large database on the server side, the data that is initially sent is just a small, initial bundle of data - like the first place you see when you open Google Maps. As the client interacts with the map, and requests different information, that Javascript code will ask the server for additional data.
Protocols
When two computers talk to each other, there is a thick sandwich of protocols that make that communication possible. These include multiple network protocols and web protocols. And sitting right on top of that thick sandwich is the HTTP protocol, which is the primary protocol used by the internet.
First, what kind of things are the client and server communicating? The client communicates things like "Can I have charlesreid1.com?" and "Can I have this piece of data from the database?" The server will communicate things like, "Here are all the files required for charlesreid1.com," or "Here is the piece of information you requested from the database."
There are two kinds of requests that a browser will send a request to an HTTP web server: a GET request, and a POST request.
GET
Think of GET requests as a very simple set of key-value pairs. These key-value pairs are actually encoded directly into the URL, so if I request a page like:
http://charlesreid1.com/page.html
then i can send along a key-value pair dictionary by adding them to the URL that I type in my browser:
http://charlesreid1.com/page.html?variable1=bananas&variable2=apples
Now the code on page.html can use these GET variables.
GET variables are used to encode simple information, like "Look at page 5," in the URL. But as the amount of information in a request gets more complex, GET doesn't work so well.
POST
The GET method obviously isn't scalable for anything but simple information. For everything else, there's POST. POST requests are sent to the server silently, behind the scenes. This allows POST to be used to transfer large amounts of data.
Combined with a format for serializing data (i.e., turning a chunk of data into a stream), POST can be used to send arbitrary data between a server and a client.
POST JSON
Enter JSON - Javascript Serial Object Notation. This is a way of taking Javascript objects (a dictionary for all you Python people out there, a Javascript object is a Python dictionary) and serializing them. A simple example of JSON looks like this:
{
"variable1" : "bananas",
"variable2" : "apples"
}
These can also be nested, and include arrays, or lists, of values:
{
"variable1" : {
"fruit" : "bananas",
"possible attacks" : ["killer bee banana attack","hidden banana peel death-squeeze"]
},
"variable2" : {
"fruit" : "apples",
"possible attacks" : ["the choker","death from above"]
}
}