How to Convert dictionary data in html table

it’s simple to show the data of a dictionary into an html file with python:

  • iterate the dictionary
  • iterate the values, if values are in al list
  • add the “<td>” tag after each value and “<tr>” after eack key into a string
  • add the “<table>” tags at the start and end
  • save the string into a file html
  • open the file
import os

a = {
	
	"First" : ["1","2"],
	"Second" : ["1","2","3"] 
}


data = ""
for k in a:
	data += "" + k + ""
	for d in a[k]:
		data += "" + d + ""
	data += ""

data = "" + data + "
" print(data) with open("file.html", "w") as file: file.write(data) os.startfile("file.html")

Result

Here is the browser render of the dictionary

Let’s make a GUI for this memo app – part 1

In the next post we will add the buttons to add or remove the memos

import os
import pickle
import tkinter as tk
import tkinter.ttk as ttk


def save_template():
	global memo
	memo = {
		
		"Da pagare" : "tim, enel",
		"Wip" : """
Skate Jack - game,
matchpair - game,
Posizionamento - article for aziendaitalia,
				"""
	}
	with open("memo.pkl", "wb") as file:
		pickle.dump(memo, file)

def save(event):
	key = lst.get(lst.curselection())
	memo[key] = tbx.get("0.0", tk.END)
	print("Changes saved")
	with open("memo.pkl", "wb") as file:
		pickle.dump(memo, file)	

def load_template():
	with open("memo.pkl", "rb") as file:
		memo = pickle.load(file)
	return memo

def save_once():
	save_template()

def to_html(event):
	data = ""
	for k in memo:
		data += "
" for d in memo[k].split(","): data += "" data += "" data = "
" + k + "" + d + "
" + data + "
" print(data) with open("file.html", "w") as file: file.write(data) os.startfile("file.html") # get data from pickle memo = load_template() # window def show(event): ''' call this to show the note in the tbx ''' mm = lst.get(lst.curselection()) tbx.delete("0.0", tk.END) tbx.insert(tk.END, memo[mm]) def add_memo(event): print("Here add a key, asking the name of the key with simpledialog") pass def help(): ''' call this to show some help ''' print(''' memo is the dictionary key = name of the event value = multiline with things to remember separated by comma lst is the listbox with the keys tbx is the textbox if you Select a key you see the values in the tbx ''') root = tk.Tk() root.title("Memo - use , to separate the items for each memo") # ==================== Change theme =============== style = ttk.Style(root) root.tk.call('source', 'azure dark\\azure dark.tcl') style.theme_use('azure') frm1 = tk.Frame(root) frm1.pack(side="left", fill="both", expand=True) lst = tk.Listbox(frm1) lst.pack(side="left", fill="both", expand=True) for k in memo: lst.insert(tk.END, k) frm2 = tk.Frame(root) frm2.pack(side="left") tbx = tk.Text(frm2) tbx.pack(side="left") lst.bind("<>", show) root.bind("", save) root.bind("", to_html) root.bind("<+>", add_memo) root.mainloop()

https://github.com/formazione/mempy

In the repository you can find the azure dark theme for this app.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.