ENGG1003_10_PythonApplicationsOnJupiter
ENGG1003_10_PythonApplicationsOnJupiter
Python Applications on
Jupyter Notebook
Outline
• Introducing Jupyter Notebook
2
Open Platform
for All
• https://jupyter.org/
• "Free software, open standards, and web services
for interactive computing across all programming
languages"
• "Project Jupyter is a non-profit, open-source
project, born out of the IPython Project in 2014 as
it evolved to support interactive data science and
scientific computing across all programming
languages"
• Both Python and R are supported
• Jupyter Notebook is a "classic" tool in data science
3
Jupyter Notebook
• Web-based notebook interface front-end
6
Setup and Run Jupyter
Notebook
• Download and install on your own computer
• Run on provided VM
• https://labs.azure.com/virtualmachines
8
Data Science Workspace on Cloud
https://app.datacamp.com/workspace
• DataCamp Workspace – web-based computing
environment through browser
9
Web-based Data
Processing with Jupyter
Notebook
• Retrieve and process online data on the fly
10
Python IDLE or Jupyter
Notebook?
• If you are building and distributing standalone
Python applications, you may work on Python IDLE
• If you are building and sharing Notebooks that can
be used on web-browsers, use Jupyter Notebook
• For the course project, you shall use Excel and/or
Jupyter Notebook
• You may perform some data analysis on Orange and
paste those results and figures into Jupyter Notebook !
11
Examples Do NOT Work ?!
• If you try the examples with Python IDLE on your own computer,
they may not work!
• Moreover, Python IDLE cannot handle Notebooks!
• Beginner's own Python IDLE installation may not have libraries and
packages, such as pandas, matplotlib, numpy, scipy, scikit-learn,
pytorch, tensorflow, etc.
import pandas as pd
header = {'User-Agent': 'Mozilla/5.0'}
ambulance_service_indicators = pd.read_csv("http://www.hkfsd.gov.hk/" +
"datagovhk/datasets/Ambulance_Service_Indicators_eng.csv",
storage_options=header)
print( ambulance_service_indicators.describe() )
Source: http://www.hkfsd.gov.hk/datagovhk/datasets/Ambulance_Service_Indicators_eng.csv 13
Reading Data from the
Web (URL)
• Use package ssl first, then pandas:
• Sacrificing security as a temporary measure:
# On Azure Labs VM Win10 or Python 3.10, there may be ERROR due to failure in
Problem resolved
verifying the web-site digital certificates on"Hongkong
issued by MS Azure Post
Labs VM by
e-Cert
SSL CA 3 - 17", here is a workaround:
import ssl technically post-installing HK Post CA Certificates !
ssl._create_default_https_context = ssl._create_unverified_context
import pandas as pd
header = {'User-Agent': 'Mozilla/5.0'}
ambulance_service_indicators = pd.read_csv("http://www.hkfsd.gov.hk/" +
"datagovhk/datasets/Ambulance_Service_Indicators_eng.csv",
storage_options=header)
print( ambulance_service_indicators.describe() )
14
Ambulance
Service
Indicators -
Data from
HK Fire
Services
Departme
nt
15
Demonstration: Plot Data
in a CSV
import pandas as pd
ambulance_service_indicators = pd.read_csv("http://www.hkfsd.gov.hk/" +
"datagovhk/datasets/Ambulance_Service_Indicators_eng.csv", storage_options=header)
print(ambulance_service_indicators[
plt.grid()
plt.show()
16
Using Jupyter Notebook on
VM
• Start Jupyter Notebook by double-clicking on the
provided desktop icon
• There will be a "Command Prompt" window appearing
• Jupyter Server and Python Kernel is running there!
• Do NOT touch it or close it!
17
Using Jupyter Notebook on
VM
• A browser window will then pop-up
• This is your workspace ( http://localhost:8888 )
• It looks like Explorer/ Finder window in your browser
• When you click [Quit], Jupyter will shutdown and the
URL ( http://localhost:8888 ) will stop working
18
Get Notebooks on VM
• Have example Notebooks ready in two ways:
19
Click to Open Notebook or
New
20
Notebook in Another
Browser Tab
21
Notebook Cell Type
Markdown cell
Markdown cell
Code cell
Code cell
22
Markdown
• Our old friend: HyperText Markup Language (HTML)
• Full featured
• Markup using <tags> for styling and formatting content
# Heading #
**Bold** _italic_
Horizontal rule:
---
[Hyperlink](http://localhost:8888)
Quote from: Wikipedia contributors, "Markdown," Wikipedia, The Free Encyclopedia,
23
https://en.wikipedia.org/w/index.php?title=Markdown&oldid=1076766851 (accessed March 22, 2022).
Code Cells
• Edit and Run Python code in cells
• The Python Kernel keeps track of the running states
in a Notebook
• Store a value in a variable, like:
x=7
• Subsequent cells can make use of x:
y=x+8
• print( ) is assumed in many cases, showing immediate
results
24
Running a Notebook
• Run All
• Run cells one-by-one
• Save / auto-save (file-type: .ipynb)
• Save and Checkpoint for version-control
• File Download as
• Export to different document formats
• Edit Insert Image
25
Using Notebook on Google
Colab
• Google Account is required
https://drive.google.com/file/d/1kukGM4uGWkAHrJvk
mkGtAUE7rah6lfdS/view?usp=sharing
27
Manage Colab Notebooks
on Google Drive
Share!
29
Summary
• Interactive computing, data science and visual
presentation with Jupyter Notebook
30