CSC 111: Introduction to Computer Science
HW 8: Python Creating HTML Files
Due Wednesday April 6, Midnight, via Moodle
SUBMIT as HW8_HTML.py
Writing a Python Program to Write an HTML file
This homework builds directly on the lab. Make sure you do the lab task before starting this homework.
The assignment is divided into steps. You should complete each step and test your code (viewing the
.html file created both in a text editor and in a web browser).
Step 1: Add your name
Use the following main() function, and modify the program from lab so that the name you assign appears in the
.html file that is created by your Python program (and so also in the web page).
def main():
name = "Kermit"
makeHTML(name)
- Write this name to the top title line. This new header text should replace the rather boring header text
of "This is a first level heading font" from the lab.
- Also include the name somewhere in the normal text on your page, which you should change to be a short
sentence of your own.
- You MUST do this using your name as a variable. You must NOT simply type your name
directly into the HTML string. This variable MUST be passed into your new makeHTML() function as shown above.
- To Do This: Look at the long string in triple quotes from the lab. Create your own text portions of this
long string. Decide where the two places are that you will insert the name. You then break this long string by
closing it part way through with triple quotes, concatentating your name variable (using regular string concatenation),
and concatenating that to the next portion of the original string, opening it again with triple quotes.
- DO NOT ask for user input. When I run your program after you have submitted it, I should be able
to run it from IDLE without needing to type anything in, and have your program create the .html file that I can then
view in a web browser.
- If you would like, you can add a "background color" via "bgcolor." View the source for this, or any other, web
page to see how this is done.
If you use "Kermit" for example,
as the name, then you want the first header line of your webpage to include your name perhaps as:
---------------------------------------------
Kermit's CSC111 Test Page
Here is some new text for Kermit's first webpage.
---------------------------------------------
Step 2: Create a function to create and open your .html file
As you add more elements to your .html file, you will be doing this by adding new functions to your .py file. To
set that up, create a new function that will simply open your .html file. As we did in class, you will probably want
to return the file variable, to be used in each subsequent function. You could also write in the opening html tags
here if you want to (or you could do it in the function that has your name as a parameter).
Make sure your program is working before you proceed.
Step 3: Add a date and time stamp to your file
Write a new function to insert a time stamp. Using the time, or datetime, library that we have already used, retrieve the date and time at
the moment you run your Python program, and put that date and time into your .html file. Format this how you like.
Step 4: Add a hyperlink to your file
Write a new function to insert a hypertext link into your html code. Using the "anchor" tags, insert a hypertext link into your
webpage. For the final program you submit DO NOT ask the user for this link, but rather simply pass the string into your new function.
Step 5: Add an image to your file
Write a new function to add the text that will add an image into your .html file. For the file that you submit, use the file name
"HW8image.jpg" for the filename of your image. For testing everyone's program, I will have one image that I use, and will assume
that everyone's .html file will look for and insert the file "HW8image.jpg."
Step 6: Close the file
This step should be self explanatory. It is up to you whether you put this in a separate function or do it in main(). Be sure that you
also write the closing tags to your .html file (closing the body and the html tags).
Step 7: Check Everything
Though you are encouraged to format your page in new and exciting ways, make sure that the final .html file can be
easily read.
-
BE CERTAIN to name the .jpg file that your .html file looks for simply HW8image.jpg. If you leave a different .jpg file
name in your code, such that when I run it for grading the file cannot be found, I will deduct points.
-
BE CERTAIN that your program does not ask for any user input. I should be able to run it simply by pressing F5 and get
the .html file without any further input from me.
-
For learning how to use functions and pass information back and forth, use the following main() function as a close
guideline for what you do. The important points here are that you need to pass in the name for step 1, and the URL for
step 4, and and the image filename for step 5. This is so that you practice passing parameters. Also note that you need
a return value for the first function, in which you open the HTML file. You will also need to pass this 'infile' variable
into the other functions - this is not shown below - you need to add this to the function calls that you actually use.
- For commenting your functions - look at the sample programs posted for this week. Put a comment INSIDE each function,
immediately after the 'def' line. This comment should be enclosed inside triple quotes (note this is a different use of the triple
quotes from how we use them for formatting the HTML code). This comment can then be viewed with the Python help
facility to provide online help for your program - make these comments useful! Note if you run the "helloFct()" file linked
on the web page, and then type >>> help ("helloFct") at the IDLE prompt, you will see the helpful comments for that program.
- Check the bottom of Lab 8 for some hints on basic HTML tags that you might want to use.
Sample main() function
You do not need to use this function exactly, but do use it as a guide. Also, be sure to add some comments.
def main():
infile = openHTML()
writeName(name)
writeTime()
addURLlink(URLname)
# Note (comment not to be included in your program) - the imageName must be "HW8image.jpg"
addJPG(imageName)
closeHTML()
A rather boring example of the kind of page you might create through your Python program can be
found here.