Chapter 2: Coding with HTML and PyScript
The focus of this chapter is to build up your skills before starting to implement the KNN algorithm in chapter 3. First, we will develop a simple website that uses PyScript.
This will be the equivalent of building your first Hello World application. After that, I will show you some examples of the power of PyScript by doing some
exercises which include Python and Numpy code. These exercises will gradually help you to better grasp coding in Python with Numpy, and will help you to better
understand how to run everything on the browser with PyScript.
The chapter has 2 basic sections. One section focuses on creating your first Hello World program. The second section consists of a set of exercises to get more comfortable
with Numpy and PyScript. I often give exercises like these to my machine learning students. I often recommend that they should type up the code themselves (instead
of copying) and run it line by line. I feel that by doing this one can develop a type of muscle memory. Hopefully, after doing many of these problems, you will
start to become more comfortable with how Numpy works with Python.
So, in summary, this chapter will focus on the following tasks:
- Build a Hello World program with HTML and PyScript
- Practice and appreciate the power of Numpy and PyScript
This chapter assumes that you have completed chapter 1, and that you have now completed the setup of your GitHub account and completed all other related configurations.
Copyright and License
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, without written permission of the copyright owner.
MIT License.
FTC and Amazon Disclaimer
This post/page/article includes Amazon Affiliate links to products. This site receives income if you purchase through these links.
This income helps support content such as this one.
Hello World program with HTML and PyScript
The following code listing shows the entire program using HTML, PyScript, and Python. So, what is Hello World anyway? Hello World is usually the first program you write in any programming language. In this listing, I show all the code you need to run Hello World. I do this so that you can see the big picture and so that you get a sense of how big it is. Running this program will write "Hello World" to your website. The results can be seen in the figure after the listing.
The listings following the next figure in this section will focus on the different parts of the Hello World rogram. It will make it easier for me to discuss what each part is doing. Your Hello World program should look like this when you finish and run it:
In the next listing, I will only discuss the head section of the code denoted by the head tags:
The title tag
is used to
provide a title for your website and is useful for search engine indexing.
The following tag
is used to define character encoding for your
website. The next 2 lines are very important and we will be using them on all our programs. They are both provided by
the PyScript project.
The line denoted by the following tag
This tag references the stylesheet which is a file with extension .css.
This style sheet just makes our website look more slick when it is running. We could do without it or create our own. But let us just use what the PyScript
project provided. CSS files are especially formatted files where we define common things realted to style about our website like fonts, positioning, colors, etc.
The last line is for javascripts as denoted by the following tag