Skip to content

Instantly share code, notes, and snippets.

@insightsbees
insightsbees / plotly_network.py
Created September 1, 2022 20:33
Use Plotly to visualize the network graph
#Use plotly to visualize the network graph created using NetworkX
#Adding edges to plotly scatter plot and specify mode='lines'
edge_trace = go.Scatter(
x=[],
y=[],
line=dict(width=1,color='#888'),
hoverinfo='none',
mode='lines')
for edge in G.edges():
@insightsbees
insightsbees / networkx.py
Created September 1, 2022 20:21
Create network graph using networkx
#Create the network graph using networkx
if uploaded_file is not None:
df=pd.read_csv(uploaded_file)
A = list(df["Source"].unique())
B = list(df["Target"].unique())
node_list = set(A+B)
G = nx.Graph() #Use the Graph API to create an empty network graph object
#Add nodes and edges to the graph object
for i in node_list:
@insightsbees
insightsbees / sidebar.py
Last active January 27, 2023 05:04
Import libraries and add widgets to sidebar
import streamlit as st
import pandas as pd
import numpy as np
import networkx as nx
import plotly.graph_objs as go
from PIL import Image
#Add a logo (optional) in the sidebar
logo = Image.open(r'C:\Users\13525\Desktop\Insights_Bees_logo.png')
st.sidebar.image(logo, width=120)
@insightsbees
insightsbees / Add background image from local.py
Last active September 6, 2022 11:10
Add background image from local
import base64
def add_bg_from_local(image_file):
with open(image_file, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
st.markdown(
f"""
<style>
.stApp {{
background-image: url(data:image/{"png"};base64,{encoded_string.decode()});
background-size: cover
@insightsbees
insightsbees / Add background image from URL.py
Last active June 25, 2022 23:52
Add background image from URL
def add_bg_from_url():
st.markdown(
f"""
<style>
.stApp {{
background-image: url("https://cdn.pixabay.com/photo/2019/04/24/11/27/flowers-4151900_960_720.jpg");
background-attachment: fixed;
background-size: cover
}}
</style>
@insightsbees
insightsbees / Fine-tune the chart.py
Created June 1, 2022 19:13
Fine-tune the chart
fig.update_layout(
title='Project Plan Gantt Chart',
bargap=0.1,
width=850,
height=500,
xaxis_title="",
yaxis_title="",
title_x=0.5,
legend_title="",
legend = dict(orientation = 'v', xanchor = "center", x = 0.92, y= 0.98), #Adjust legend position
@insightsbees
insightsbees / Create a Gantt Chart.py
Last active June 1, 2022 19:07
Create a Gantt Chart
colors = {}
colors['Planned'] = 'rgb(29, 133, 60)' #specify the color for the 'planned' schedule bars
colors['Actual'] = 'rgb(245, 148, 22)' #specify the color for the 'actual' schedule bars
fig = px.timeline(
df,
x_start="Start",
x_end="Finish",
y="Task",
color='Duration Type',
@insightsbees
insightsbees / Import libraries and read data.py
Last active June 1, 2022 18:51
Import libraries and read data
import pandas as pd
import numpy as np
import plotly.express as px
df=pd.read_csv(r'C:\Users\13525\Desktop\Insights Bees\Gantt_chart_multi_layer\Data\project_plan.csv')
df['Start'] = df['Start'].astype('datetime64')
df['Finish'] = df['Finish'].astype('datetime64')
@insightsbees
insightsbees / Reshape from wide to long.py
Last active May 24, 2022 16:13
Reshape from wide to long
year_list=list(df_wide.columns)
df_long = pd.melt(df_wide, value_vars=year_list,value_name='Avg. Price ($)', ignore_index=False)
@insightsbees
insightsbees / Reshape from long to wide.py
Last active May 24, 2022 14:21
Reshape from long to wide
df_wide=pd.pivot(df, index=['Series ID','Item'], columns = 'Year Month',values = 'Avg. Price ($)') #Reshape from long to wide
#Re-arange the new columns in the correct order
cols = df['Year Month'].unique()
df_wide=df_wide[cols]