Saturday, March 18, 2017

Start Python programming: How Python 3 works for websites

We test codes on this website: https://repl.it/languages/python3

Here we will learn how we can control HTML document with the server-side script "Python". You might think it would be difficult but actually it is not so difficult.

Please write:
print("Hello World")
By the way, print("") is used to display some words. See the result below.

click to expand


Then click "Run". You will see "Hello World" will appear on the console. ("Console" means the black window next to the editor window)
click to expand


Yes, "Hello World" is displayed... but you would wonder like this "yes, hello world appeared on the console... but so what?" This seems like a small step but actually this is a very big leap for your web programming.

Write this way now:
print("<h1>Welcome to my website</h1>")

click to expand

Look at the console. You can see it is displaying "<h1>Welcome to my website</h1>". If this is executed on a server, this "<h1>Welcome to my website</h1>" is shown to each browser that accessed to your website.

But the browser will see it as a HTML page as long as it has HTML structure. <h1></h1> is HTML tag to make big letters. So the result on each browser will be as follows:



This means you can dynamically adjust and control HTML document before it is rendered in each browser. See an example below:
code:
print("<h1>Welcome to my website</h1>") 
amount = 100.0 
if amount < 100:
  discount = amount * 0.05
elif amount < 500:
  discount = amount * 0.10
else:
  discount = amount * 0.15 
print ("<p>Your amount: ",amount,"</p>")
print ("<p>Discount: ",discount, "</p>")
print ("<p>Net payable: ",amount-discount,"</p>")



And the result of this code will be 

If this is rendered as HTML document in a browser, this would be shown as:



You can see that this HTML document will vary depending on the amount value. If the "amount" value is changed to "120" from "100", then the result is:

So HTML content shown in a browser is also changed:
You can see each value is changed

This is how server-side programming (or server-side scripting) is done.

Start Python programming

Python is a server-side programming language. What does server-side mean? There are 2 kinds of languages in web programming. One is server-side programming language, the other is client-side programming language. Server-side language works in a server (to affect website itself), meanwhile, client-side language works in user's browsers (to react to user's behavior on a website).

For example, Javascript is a client-side programming language that enables to interact with people's behavior on a website. For example, if you want to make photos twice as big when photos are clicked on a website, you would need to use Javascript.

The image below shows how client-side language (like Javascript) works. As this image shows, yes client-side work in browser depending on user's behavior. If a you click a button on a website, Javascript (or CSS, HTML) will detect the behavior (and start a new activity).

click to expand

Meanwhile, Python is a server side language, so Python works inside a server so that Python can change the website dynamically. For example, Google's search-result always changes depending on what word you searched for. This is because a server-side program(1) dynamically changes the result page depending on what word the server received.
click to expand

To start programming with Python, you don't need to prepare anything unlike Java or C, C++, C#. Just write codes with Python on some editor and upload it to a server (in which Pyrhon is installed). Then the server automatically execute the Python file (as a website or webapp) every time users access to the page. If you use a rental server that has been Python installed, you don't need to even install Python by yourself because it should have been installed beforehand by the server administrator. (By the way, some rental servers don't have Python or have only older version's Python installed, so please be careful when choosing a rental server for your website.)

In short. You want to change your website dynamically depending on the data of a server? Use server-side languages. You want to change the website depending on user's behavior? Use client-side languages. You want both? Use both of them together.

(1) Programs works in a server, which are usually made by server-side language (like Python), are called server-side programs.

Sunday, March 12, 2017

Make a Search engine with PHP & MySQL

You can search research papers with this search engine.

Paper search
http://searcheverythin.carrots.jp/

This website is a opensource project. The code is:
https://github.com/shunakanishi/FreeSearchEngine