Created
January 4, 2025 14:47
-
-
Save fisherds/9a92bf563430c78bdc3ad111a3df352b to your computer and use it in GitHub Desktop.
Example of generating server side content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def generate_dynamic_response_body(dict): | |
#build up string programmatically | |
response_body = """ | |
<html> | |
<head> | |
<title>Dynamic Demo</title> | |
<link rel="stylesheet" href="styles/style.css"> | |
</head> | |
<body> | |
<h1>Rendering Server-Side Variables Dynamically</h1> | |
<ul> | |
""" | |
#append enough list items based on the provided dictionary | |
# TODO add in a <li> with the key value pair for each entry in the dict | |
for key in dict.keys(): | |
response_body+= f"<li>{key}: {dict[key]}</li>\r\n" | |
#close the ul, body, html for a completed html page | |
response_body += """ | |
</ul> | |
</body> | |
</html> | |
""" | |
#prepared to write to the browser | |
response_body = bytearray(response_body, encoding = "utf-8") | |
return response_body | |
#add random entry to dictionary that does not already exist | |
def add_random_entry(dict): | |
rnd = random.randint(1,1000) | |
i=0 | |
while ( f"r{i}" in dict): | |
i+=1 | |
dict[f"r{i}"] = rnd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment