SlideShare a Scribd company logo
1 of 25
Python and Web
Programming
November 21, Unit 8
Web Scripts
• Programs that generate web pages are often
called web scripts or CGI scripts
• Like using XSL to transform an XML file into an
HTML file, we can use web scripts to create web
pages for us
• Why use web scripts?
– Allows use to dynamically generate web pages
• These web pages are created upon request
– Standard HTML files stored on a server are called
static web pages or static files
• They don’t really change (unless we edit the HTML)
Creating Web Pages
• We can create web pages using a script in
many different languages
• Obviously, we can use Python (since that’s
what we’re learning)
• Other common languages include:
– Perl
– PHP
– ASP
Simple Page in Python
• Creating a simple web page in Python is very
similar to how it was done using XSL
• Basically, the Python program will need to output
the HTML we need for our page
• To do this, we’ll use a series of print statements
• The basic things we need are:
– Header information (text/html)
– A blank line
– <html>, <head>, <title>,<body>, HTML content, and
closing tags
Simple Python Script
print “Content-type: text/html”
print
print ”<html><head>”
print “<title>Python Example</title>”
print ”</head><body>”
print “<h1>Python is fun</h1>”
print “<p> Python made this page</p>”
print “</body></html> “
Header Information
• print “Content-type: text/html”
• This produces the header information for our
web page
• Basically the purpose of this is to tell the browser
which type of information is going to be sent
– Indicates the MIME type
• When we put a python script online, it has the
extension “.py”
– Browser needs to know that HTML is coming
• Why the blank line after the header?
– Indicates the start of the HTML
Triple-Quoted Strings
• When we create web pages with Python
we can dynamically change parts of the
page
– But large portions of the page may be static
• Instead of having multiple print statements
we can use fewer and enclose much of
our HTML in triple quoted strings “’
– Allows us to have line breaks where double
quotes do not in python
Triple-Quoted String Example
print “Content-type: text/html”
print
print ”””<html><head>
<title>Python Example</title>
</head><body>
<h1>Python is fun</h1>
<p> Python made this page</p>
<body></html> “””
• This produces the same output as the first example
Python script
• Allows us to copy and paste large chunks of HTML
Python Generated Web Pages
• The previous scripts are kind of pointless
to do with Python
– They actually create static web pages
– They don’t change
• But we can generate web pages that do
change
• A simple example is to include the current
time on a web page
Adding Time to a Web Page
import time
print “Content-type: text/html”
print
print “’<html><head><title>Time
Page</title></head>”’
print”<body><p>Right now, the time is <strong> “
print time.asctime(), “</strong></p>”
Print “</body></html>”
Output From Last Script
Content-type: text/html
<html><head><title>Time Page</title></head>
<body><p>Right now, the time is <strong>Tue
Nov 21 13:45:00 2005 </strong></p>
</body></html>
Running Python Scripts
• When we run this script locally, we see the
output from our script
– What we have on the last slide
– We don’t see the web page we wanted to create
• To get the page to display we need to upload it
to the cmpt165 server
– Our webpage will have the URL path of somepage.py
• We can test our use of the timeasc function by
running it in IDLE
– The output will show up in the window
raw_data vs. Forms
• Right now we’re using raw_data to get user input
• But if we’re using a web page, we have no
console for the user to enter data into
• Instead we’ll use forms
• Forms allow users to enter data into a web page
– This data is then sent back to the web script on the
web server
• We are familiar with forms
– We enter information into a form when we use a
search engine
– Purchase items online
Building a Basic Page with a Form
• The first thing we need is the <form> tag
• The <form> tag goes around the entire
form
– Does not affect the appearance of the page
– Just notes where the form begins and ends
– Just like writing standard HTML
• <form> needs an attribute called “action”
– This specifies the action to be taken when the
form is submitted
<form>, cont.
• The action attribute is very important
– Why have a form that does nothing?
• action specifies which script to run when
the form is submitted
• The value for action should be the URL of
the script
• <form action = “testScript.py”>
• <form action =
“someFolder/customerInfo.py”>
Adding Controls
• Controls are those elements which we are going
to add to the form
– Text fields
– Text areas
– Check boxes
– Radio Buttons
– Selectable Lists
– Submit button
• A form without controls is pointless
– There’s no user input!
• We are going to add controls using the <input>
tag
<input> Tag
• With the <input> tag we need to specify
two attributes:
– type
– name
• Type attribute specifies the type of control
• Name attribute gives a name to the control
so we can access the data in it later
– Like giving it a variable name
Type Attribute
• Again, the type attribute specifies the type
of control
• Some examples include:
– type =“text” - text box
– type =“checkbox” –check box
– type =“radio” –radio button
– type = “submit” –submit button
• Example:
<input type =“submit”…..
Name Attribute
• We have to have a name to access the
information in a particular portion of the form
– Very similar to naming variables
• It’s best if the name relates to the content of the
control and the control type
– Ex.
• A text box control which gets a customer name could have a
name= “customerName”
• A check box control which allows customer to select getting
product updates name=“checkUpdates”
Value Attribute
• Sometimes we’ll also want to add a value attribute
• The value attribute specifies the default value for that
control
• Ex.
– <input type=“text” value=“a textbox” name=“textbox1”/>
• “a textbox” would appear in the text box
• Checkboxes can use the checked property to indicate if
it should be checked by default
– <input type=“checkbox” checked=“checked”
name=“checkbox1”/>
– If checked=“checked” then the box will appear checked
– “checked” is the only value that the checked property can have
Other Useful Text Attributes
• size attribute indicates how wide the text
box should be
<input type = “text” name=“text1” size =“10”/>
– Size indicates how many characters, in this
case 10
• maxlength is just like size except that it
indicates the maximum number of
characters a text box will accommodate
Submit Type
• We have to have a way to send the form
information to the script that’s going to handle it
• To do this we can use a control with the type
“submit”
• It will show up as a button
• The value attribute will indicate the text which
should be displayed on the button
<input type=“submit” value=“Go”
name=“submitButton”/>
HTML Form Example
In class Example
What’s Next?
• So now we can create forms
• And users can enter information
• But how the heck do we use it?
• We are going to use CGI to take the
information from the form and process it
somehow
– We’ll get into this next class
• You’ll be using CGI to take order
information
Questions
• What you should know:
– What web scripts are
– How to create a webpage using Python
– How to use triple quoted strings
– How to write a form in HTML
• <form action=“someprogram.py”>
• <input>

More Related Content

Similar to python and web for data science prfrograminh

Html advanced-reference-guide for creating web forms
Html advanced-reference-guide for creating web formsHtml advanced-reference-guide for creating web forms
Html advanced-reference-guide for creating web formssatish 486
 
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJSBasic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJSDeepak Upadhyay
 
Intro to ExpressionEngine and CodeIgniter
Intro to ExpressionEngine and CodeIgniterIntro to ExpressionEngine and CodeIgniter
Intro to ExpressionEngine and CodeIgniterbrightrocket
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptxSherinRappai
 
Gitika html ppt
Gitika html pptGitika html ppt
Gitika html pptgitika -
 
SUGUK Cambridge - Display Templates & JSLink for IT Pros
SUGUK Cambridge - Display Templates & JSLink for IT ProsSUGUK Cambridge - Display Templates & JSLink for IT Pros
SUGUK Cambridge - Display Templates & JSLink for IT ProsPaul Hunt
 
FormL13.pptx
FormL13.pptxFormL13.pptx
FormL13.pptxserd4
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Gheyath M. Othman
 
Web forms and html lecture Number 4
Web forms and html lecture Number 4Web forms and html lecture Number 4
Web forms and html lecture Number 4Mudasir Syed
 
HTML 5_cfbe4b8d-092saawww24bb33cb2358.pptx
HTML 5_cfbe4b8d-092saawww24bb33cb2358.pptxHTML 5_cfbe4b8d-092saawww24bb33cb2358.pptx
HTML 5_cfbe4b8d-092saawww24bb33cb2358.pptxTEJASARGADE5
 
SPSSTHLM - Using JSLink and Display Templates for ITPros
SPSSTHLM - Using JSLink and Display Templates for ITProsSPSSTHLM - Using JSLink and Display Templates for ITPros
SPSSTHLM - Using JSLink and Display Templates for ITProsPaul Hunt
 
Getting Information through HTML Forms
Getting Information through HTML FormsGetting Information through HTML Forms
Getting Information through HTML FormsMike Crabb
 
#SPSLondon - Session 2 JSLink for IT Pros
#SPSLondon - Session 2 JSLink for IT Pros#SPSLondon - Session 2 JSLink for IT Pros
#SPSLondon - Session 2 JSLink for IT ProsPaul Hunt
 
css and Input attributes
css and Input attributescss and Input attributes
css and Input attributesSiji P
 
Nsc 2013 06-17 - random rants on 2013
Nsc 2013 06-17 - random rants on 2013Nsc 2013 06-17 - random rants on 2013
Nsc 2013 06-17 - random rants on 2013Mikael Svenson
 

Similar to python and web for data science prfrograminh (20)

Chapter 9: Forms
Chapter 9: FormsChapter 9: Forms
Chapter 9: Forms
 
Html advanced-reference-guide for creating web forms
Html advanced-reference-guide for creating web formsHtml advanced-reference-guide for creating web forms
Html advanced-reference-guide for creating web forms
 
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJSBasic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
 
Intro to ExpressionEngine and CodeIgniter
Intro to ExpressionEngine and CodeIgniterIntro to ExpressionEngine and CodeIgniter
Intro to ExpressionEngine and CodeIgniter
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
Gitika html ppt
Gitika html pptGitika html ppt
Gitika html ppt
 
5. Frames & Forms.pdf
5. Frames & Forms.pdf5. Frames & Forms.pdf
5. Frames & Forms.pdf
 
SUGUK Cambridge - Display Templates & JSLink for IT Pros
SUGUK Cambridge - Display Templates & JSLink for IT ProsSUGUK Cambridge - Display Templates & JSLink for IT Pros
SUGUK Cambridge - Display Templates & JSLink for IT Pros
 
FormL13.pptx
FormL13.pptxFormL13.pptx
FormL13.pptx
 
U2A3
U2A3U2A3
U2A3
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
 
Web forms and html lecture Number 4
Web forms and html lecture Number 4Web forms and html lecture Number 4
Web forms and html lecture Number 4
 
HTML 5_cfbe4b8d-092saawww24bb33cb2358.pptx
HTML 5_cfbe4b8d-092saawww24bb33cb2358.pptxHTML 5_cfbe4b8d-092saawww24bb33cb2358.pptx
HTML 5_cfbe4b8d-092saawww24bb33cb2358.pptx
 
SPSSTHLM - Using JSLink and Display Templates for ITPros
SPSSTHLM - Using JSLink and Display Templates for ITProsSPSSTHLM - Using JSLink and Display Templates for ITPros
SPSSTHLM - Using JSLink and Display Templates for ITPros
 
CSS_Forms.pdf
CSS_Forms.pdfCSS_Forms.pdf
CSS_Forms.pdf
 
Getting Information through HTML Forms
Getting Information through HTML FormsGetting Information through HTML Forms
Getting Information through HTML Forms
 
#SPSLondon - Session 2 JSLink for IT Pros
#SPSLondon - Session 2 JSLink for IT Pros#SPSLondon - Session 2 JSLink for IT Pros
#SPSLondon - Session 2 JSLink for IT Pros
 
Lab1_HTML.pptx
Lab1_HTML.pptxLab1_HTML.pptx
Lab1_HTML.pptx
 
css and Input attributes
css and Input attributescss and Input attributes
css and Input attributes
 
Nsc 2013 06-17 - random rants on 2013
Nsc 2013 06-17 - random rants on 2013Nsc 2013 06-17 - random rants on 2013
Nsc 2013 06-17 - random rants on 2013
 

More from Rajasekhar364622

functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptRajasekhar364622
 
Machine learning how are things going on
Machine learning how are things going onMachine learning how are things going on
Machine learning how are things going onRajasekhar364622
 
Using Tree algorithms on machine learning
Using Tree algorithms on machine learningUsing Tree algorithms on machine learning
Using Tree algorithms on machine learningRajasekhar364622
 
DBMS-material for b.tech students to learn
DBMS-material for b.tech students to learnDBMS-material for b.tech students to learn
DBMS-material for b.tech students to learnRajasekhar364622
 
unit5-academic writing human intraction.ppt
unit5-academic writing human intraction.pptunit5-academic writing human intraction.ppt
unit5-academic writing human intraction.pptRajasekhar364622
 
Advantages and disadvantages of ML .PPTX
Advantages and disadvantages of ML .PPTXAdvantages and disadvantages of ML .PPTX
Advantages and disadvantages of ML .PPTXRajasekhar364622
 
BLOCK CHAIN technology for the students.
BLOCK CHAIN technology for the students.BLOCK CHAIN technology for the students.
BLOCK CHAIN technology for the students.Rajasekhar364622
 
wepik-green-innovations-paving-the-way-for-a-sustainable-future-2023120414035...
wepik-green-innovations-paving-the-way-for-a-sustainable-future-2023120414035...wepik-green-innovations-paving-the-way-for-a-sustainable-future-2023120414035...
wepik-green-innovations-paving-the-way-for-a-sustainable-future-2023120414035...Rajasekhar364622
 

More from Rajasekhar364622 (8)

functions modules and exceptions handlings.ppt
functions modules and exceptions handlings.pptfunctions modules and exceptions handlings.ppt
functions modules and exceptions handlings.ppt
 
Machine learning how are things going on
Machine learning how are things going onMachine learning how are things going on
Machine learning how are things going on
 
Using Tree algorithms on machine learning
Using Tree algorithms on machine learningUsing Tree algorithms on machine learning
Using Tree algorithms on machine learning
 
DBMS-material for b.tech students to learn
DBMS-material for b.tech students to learnDBMS-material for b.tech students to learn
DBMS-material for b.tech students to learn
 
unit5-academic writing human intraction.ppt
unit5-academic writing human intraction.pptunit5-academic writing human intraction.ppt
unit5-academic writing human intraction.ppt
 
Advantages and disadvantages of ML .PPTX
Advantages and disadvantages of ML .PPTXAdvantages and disadvantages of ML .PPTX
Advantages and disadvantages of ML .PPTX
 
BLOCK CHAIN technology for the students.
BLOCK CHAIN technology for the students.BLOCK CHAIN technology for the students.
BLOCK CHAIN technology for the students.
 
wepik-green-innovations-paving-the-way-for-a-sustainable-future-2023120414035...
wepik-green-innovations-paving-the-way-for-a-sustainable-future-2023120414035...wepik-green-innovations-paving-the-way-for-a-sustainable-future-2023120414035...
wepik-green-innovations-paving-the-way-for-a-sustainable-future-2023120414035...
 

Recently uploaded

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfJerry Chew
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 

Recently uploaded (20)

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 

python and web for data science prfrograminh

  • 2. Web Scripts • Programs that generate web pages are often called web scripts or CGI scripts • Like using XSL to transform an XML file into an HTML file, we can use web scripts to create web pages for us • Why use web scripts? – Allows use to dynamically generate web pages • These web pages are created upon request – Standard HTML files stored on a server are called static web pages or static files • They don’t really change (unless we edit the HTML)
  • 3. Creating Web Pages • We can create web pages using a script in many different languages • Obviously, we can use Python (since that’s what we’re learning) • Other common languages include: – Perl – PHP – ASP
  • 4. Simple Page in Python • Creating a simple web page in Python is very similar to how it was done using XSL • Basically, the Python program will need to output the HTML we need for our page • To do this, we’ll use a series of print statements • The basic things we need are: – Header information (text/html) – A blank line – <html>, <head>, <title>,<body>, HTML content, and closing tags
  • 5. Simple Python Script print “Content-type: text/html” print print ”<html><head>” print “<title>Python Example</title>” print ”</head><body>” print “<h1>Python is fun</h1>” print “<p> Python made this page</p>” print “</body></html> “
  • 6. Header Information • print “Content-type: text/html” • This produces the header information for our web page • Basically the purpose of this is to tell the browser which type of information is going to be sent – Indicates the MIME type • When we put a python script online, it has the extension “.py” – Browser needs to know that HTML is coming • Why the blank line after the header? – Indicates the start of the HTML
  • 7. Triple-Quoted Strings • When we create web pages with Python we can dynamically change parts of the page – But large portions of the page may be static • Instead of having multiple print statements we can use fewer and enclose much of our HTML in triple quoted strings “’ – Allows us to have line breaks where double quotes do not in python
  • 8. Triple-Quoted String Example print “Content-type: text/html” print print ”””<html><head> <title>Python Example</title> </head><body> <h1>Python is fun</h1> <p> Python made this page</p> <body></html> “”” • This produces the same output as the first example Python script • Allows us to copy and paste large chunks of HTML
  • 9. Python Generated Web Pages • The previous scripts are kind of pointless to do with Python – They actually create static web pages – They don’t change • But we can generate web pages that do change • A simple example is to include the current time on a web page
  • 10. Adding Time to a Web Page import time print “Content-type: text/html” print print “’<html><head><title>Time Page</title></head>”’ print”<body><p>Right now, the time is <strong> “ print time.asctime(), “</strong></p>” Print “</body></html>”
  • 11. Output From Last Script Content-type: text/html <html><head><title>Time Page</title></head> <body><p>Right now, the time is <strong>Tue Nov 21 13:45:00 2005 </strong></p> </body></html>
  • 12. Running Python Scripts • When we run this script locally, we see the output from our script – What we have on the last slide – We don’t see the web page we wanted to create • To get the page to display we need to upload it to the cmpt165 server – Our webpage will have the URL path of somepage.py • We can test our use of the timeasc function by running it in IDLE – The output will show up in the window
  • 13. raw_data vs. Forms • Right now we’re using raw_data to get user input • But if we’re using a web page, we have no console for the user to enter data into • Instead we’ll use forms • Forms allow users to enter data into a web page – This data is then sent back to the web script on the web server • We are familiar with forms – We enter information into a form when we use a search engine – Purchase items online
  • 14. Building a Basic Page with a Form • The first thing we need is the <form> tag • The <form> tag goes around the entire form – Does not affect the appearance of the page – Just notes where the form begins and ends – Just like writing standard HTML • <form> needs an attribute called “action” – This specifies the action to be taken when the form is submitted
  • 15. <form>, cont. • The action attribute is very important – Why have a form that does nothing? • action specifies which script to run when the form is submitted • The value for action should be the URL of the script • <form action = “testScript.py”> • <form action = “someFolder/customerInfo.py”>
  • 16. Adding Controls • Controls are those elements which we are going to add to the form – Text fields – Text areas – Check boxes – Radio Buttons – Selectable Lists – Submit button • A form without controls is pointless – There’s no user input! • We are going to add controls using the <input> tag
  • 17. <input> Tag • With the <input> tag we need to specify two attributes: – type – name • Type attribute specifies the type of control • Name attribute gives a name to the control so we can access the data in it later – Like giving it a variable name
  • 18. Type Attribute • Again, the type attribute specifies the type of control • Some examples include: – type =“text” - text box – type =“checkbox” –check box – type =“radio” –radio button – type = “submit” –submit button • Example: <input type =“submit”…..
  • 19. Name Attribute • We have to have a name to access the information in a particular portion of the form – Very similar to naming variables • It’s best if the name relates to the content of the control and the control type – Ex. • A text box control which gets a customer name could have a name= “customerName” • A check box control which allows customer to select getting product updates name=“checkUpdates”
  • 20. Value Attribute • Sometimes we’ll also want to add a value attribute • The value attribute specifies the default value for that control • Ex. – <input type=“text” value=“a textbox” name=“textbox1”/> • “a textbox” would appear in the text box • Checkboxes can use the checked property to indicate if it should be checked by default – <input type=“checkbox” checked=“checked” name=“checkbox1”/> – If checked=“checked” then the box will appear checked – “checked” is the only value that the checked property can have
  • 21. Other Useful Text Attributes • size attribute indicates how wide the text box should be <input type = “text” name=“text1” size =“10”/> – Size indicates how many characters, in this case 10 • maxlength is just like size except that it indicates the maximum number of characters a text box will accommodate
  • 22. Submit Type • We have to have a way to send the form information to the script that’s going to handle it • To do this we can use a control with the type “submit” • It will show up as a button • The value attribute will indicate the text which should be displayed on the button <input type=“submit” value=“Go” name=“submitButton”/>
  • 23. HTML Form Example In class Example
  • 24. What’s Next? • So now we can create forms • And users can enter information • But how the heck do we use it? • We are going to use CGI to take the information from the form and process it somehow – We’ll get into this next class • You’ll be using CGI to take order information
  • 25. Questions • What you should know: – What web scripts are – How to create a webpage using Python – How to use triple quoted strings – How to write a form in HTML • <form action=“someprogram.py”> • <input>