Skip to content

Instantly share code, notes, and snippets.

@kitsamho
Last active June 20, 2023 19:28
Show Gist options
  • Select an option

  • Save kitsamho/91fb12d96d186b9a58a066d7b8b488ff to your computer and use it in GitHub Desktop.

Select an option

Save kitsamho/91fb12d96d186b9a58a066d7b8b488ff to your computer and use it in GitHub Desktop.
def cache_d3_network_plot(df_d3_masked: pd.DataFrame, file_path: str, edge_distance: int = 100,
node_size: int = 4, fontsize: int = 8) -> None:
"""
Caches a D3 network plot to a file.
Args:
df_d3_masked: The DataFrame containing masked edge data.
file_path: The path to save the cached plot file.
edge_distance: The distance between nodes in the plot.
node_size: The size of the nodes in the plot.
fontsize: The font size of the nodes in the plot.
"""
d3 = D3Blocks()
d3.d3graph(df_d3_masked)
d3.D3graph.set_edge_properties(directed=False, edge_distance=edge_distance)
d3.D3graph.set_node_properties(color='cluster', size=node_size, edge_size=0.2, fontsize=fontsize)
d3.D3graph.show(filepath=file_path)
def load_cached_file(save_path: str) -> str:
"""
Load a file from a specified path.
Args:
save_path: Path to the file.
Returns:
The content of the file as a string.
"""
with open(save_path, "r") as f:
file = f.read()
return file
def plot_html(cached_html: str, image_size: Tuple[int, int] = (950, 550)) -> None:
"""
Displays a cached HTML plot in Streamlit.
Args:
cached_html: The path to the cached HTML plot file.
image_size: The size of the displayed image in Streamlit.
"""
st.components.v1.html(cached_html, width=image_size[0], height=image_size[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment