Skip to content
Snippets Groups Projects
Commit a4737065 authored by msyamkumar's avatar msyamkumar
Browse files

Lec 32 materials

parent 38593c46
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
```
%% Output
%% Cell type:code id: tags:
``` python
import csv
import os
import csv
```
%% Cell type:code id: tags:
``` python
# copied from https://automatetheboringstuff.com/2e/chapter16/
def process_csv(filename):
exampleFile = open(filename)
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader)
return exampleData
```
%% Cell type:markdown id: tags:
## Example 1: List Visualization
### Write a gen_html function
- Input: shopping_list and path to shopping.html
- Outcome: create shopping.html file
### Pseudocode
1. Open "shopping.html" in write mode.
2. Write \<ul\> tag into the html file
3. Iterate over each item in shopping list.
4. Write each item with <\li\> tag.
5. After you are done iterating, write \</ul\> tag.
6. Close the file object.
%% Cell type:code id: tags:
``` python
def gen_html(shopping_list, html_path):
f = open(html_path, "w")
f.write("<ul>\n")
for item in shopping_list:
f.write("<li>" + str(item) + "\n")
f.write("</ul>\n")
f.close()
gen_html(["apples", "oranges", "milk", "banana"], "shopping.html")
```
%% Cell type:markdown id: tags:
## Example 2: Dictionary Visualization
### Write a csv_to_html function
- Input: path to review1.csv and path to reviews.html
- Outcome 1: create a html file for each review
- Outcome 2: create reviews.html file containing link to a html file for each review
### Pseudocode
1. Create data_html folder using os.mkdir. Make sure to use try ... except blocks to catch FileExistsError
2. Use process_csv function to read csv data and split the header and the data
3. For each review, extract review id, review title, review text.
4. generate the \<rid\>.html for each review inside data_html folder.
- Open \<rid\>.html in write mode
- Add review title using \<h1\> tag
- Add review text inside\<p\> tag
- Close \<rid\>.html file object
5. generate a reviews.html file which has link to each review html page \<rid\>.html
- Open reviews.html file in write mode
- Add each \<rid\>.html as hyperlink using \<a\> tag.
- Close reviews.html file
%% Cell type:code id: tags:
``` python
def csv_to_html(csv_path, html_path):
try:
os.mkdir("data_html")
except FileExistsError:
pass
reviews_data = process_csv(csv_path)
reviews_header = reviews_data[0]
reviews_data = reviews_data[1:]
reviews_file = open(html_path, "w")
reviews_file.write("<ul>\n")
for row in reviews_data:
rid = row[reviews_header.index("review id")]
title = row[reviews_header.index("review title")]
text = row[reviews_header.index("review text")]
# STEP 4: generate the <rid>.html for each review inside data folder
review_path = os.path.join("data_html", str(rid) + ".html")
html_file = open(review_path, "w")
html_file.write("<h1>{}</h1><p>{}</p>".format(title, text))
html_file.close()
# STEP 5: generate a reviews.html file which has link to each review html page <rid>.html
reviews_file.write('<li><a href = "{}">{}</a>'.format(review_path, str(rid) + ":" + str(title)) + "<br>\n")
reviews_file.write("</ul>\n")
reviews_file.close()
csv_to_html(os.path.join("data", "review1.csv"), "reviews.html")
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
```
%% Cell type:code id: tags:
``` python
import csv
import os
```
%% Cell type:markdown id: tags:
## Example 1: List Visualization
### Write a gen_html function
- Input: shopping_list and path to shopping.html
- Outcome: create shopping.html file
### Pseudocode
1. Open "shopping.html" in write mode.
2. Write \<ul\> tag into the html file
3. Iterate over each item in shopping list.
4. Write each item with \<li\> tag.
5. After you are done iterating, write \</ul\> tag.
6. Close the file object.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
## Example 2: Dictionary Visualization
### Write a csv_to_html function
- Input: path to review1.csv and path to reviews.html
- Outcome 1: create a html file for each review
- Outcome 2: create reviews.html file containing link to a html file for each review
### Pseudocode
1. Create data_html folder using os.mkdir. Make sure to use try ... except blocks to catch FileExistsError
2. Use process_csv function to read csv data and split the header and the data
3. For each review, extract review id, review title, review text.
4. generate the \<rid\>.html for each review inside data_html folder.
- Open \<rid\>.html in write mode
- Add review title using \<h1\> tag
- Add review text inside\<p\> tag
- Close \<rid\>.html file object
5. generate a reviews.html file which has link to each review html page \<rid\>.html
- Open reviews.html file in write mode
- Add each \<rid\>.html as hyperlink using \<a\> tag.
- Close reviews.html file
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment