import networkx as nx
import matplotlib.pyplot as plt
import base64
from io import BytesIO
from IPython.display import HTML
def create_kite_network():
"""
Creates the Krackhardt kite network and returns a base64 encoded image for Quarto.
Returns: HTML : IPython.display.HTML
"""
G = nx.Graph()
nodes = ['Andre', 'Beverly', 'Carol', 'Diane', 'Ed', 'Fernando', 'Garth', 'Heather', 'Ike', 'Jane']
G.add_nodes_from(nodes)
edges = [
('Andre', 'Carol'), ('Andre', 'Diane'), ('Andre', 'Beverly'),
('Beverly', 'Diane'), ('Beverly', 'Ed'),
('Carol', 'Diane'), ('Carol', 'Fernando'),
('Diane', 'Ed'), ('Diane', 'Fernando'), ('Diane', 'Garth'),
('Ed', 'Garth'),
('Fernando', 'Garth'), ('Fernando', 'Heather'),
('Garth', 'Heather'),
('Heather', 'Ike'),
('Ike', 'Jane')
]
G.add_edges_from(edges)
pos = nx.spring_layout(G, k=1, iterations=50)
plt.figure(figsize=(10, 8))
nx.draw(G, pos,
with_labels=True,
node_color='lightgray',
node_size=1500,
font_size=10,
font_weight='bold',
edge_color='purple',
width=2)
plt.title("Krackhardt Kite Network")
plt.axis('off')
#This is hack, quarto to rpubs doesn't do images, you have to encode it into the html file directly.
buf = BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=300)
plt.close()
img_base64 = base64.b64encode(buf.getvalue()).decode('utf-8')
img_html = f'<img src="data:image/png;base64,{img_base64}" alt="Krackhardt Kite Network" />'
return HTML(img_html)
network_plot = create_kite_network()
network_plot