# LinkedIn example
from typing import List, Optional
import pandas as pd
import plotly.graph_objects as go
import vizro.models as vm
import vizro.plotly.express as px
from plotly.subplots import make_subplots
from vizro import Vizro
from vizro._themes._color_values import COLORS
from vizro.actions import filter_interaction
from vizro.models.types import capture
df = pd.read_csv("Connections 2.csv")
df.dropna(subset=["Position", "Company"], inplace=True)
df["Connected On"] = pd.to_datetime(df["Connected On"], format="%d-%b-%y")
df.set_index("Connected On", drop=False, inplace=True)
df["Year"] = df["Connected On"].dt.year
@capture("graph")
def role_chart(data_frame, top_n=20):
# Plot the most common roles for my connections
data_frame = data_frame['Position'].value_counts().nlargest(top_n).reset_index(name="Frequency")
data_frame = data_frame.sort_values(by='Frequency', ascending=True)
return px.bar(data_frame, x='Frequency', y='Position', title=f'Top {top_n} most frequent Positions')
@capture("graph")
def company_chart(data_frame, top_n=20):
# Plot the most common companies among my connections
data_frame = data_frame['Company'].value_counts().nlargest(top_n).reset_index(name="Count")
fig = px.pie(data_frame, names='Company', values='Count', title=f'Top {top_n} most connected Companies', hole=0.3)
fig.update_layout(legend=dict(x=-0.5, y=1)) # Move the legend to the top-left corner
return fig
@capture("graph")
def growth_cumulative_chart(data_frame):
data_frame = data_frame.groupby('Year').size().reset_index(name="Count per Year")
data_frame['Cumulative Yearly Connections'] = data_frame['Count per Year'].cumsum()
fig_line = px.line(data_frame, x="Year", y='Cumulative Yearly Connections')
fig_bar = px.bar(data_frame, x='Year', y='Count per Year', title='Yearly growth of my connections')
fig_bar.add_traces(fig_line.data)
return fig_bar
page = vm.Page(
title="LinkedIn data",
layout=vm.Layout(grid=[
[0, 1],
[0, 2]
]),
components=[
vm.Graph(id="role_chart_id", figure=role_chart(data_frame=df)),
vm.Graph(id="company_chart_id",figure=company_chart(data_frame=df)),
vm.Graph(figure=growth_cumulative_chart(data_frame=df)),
],
controls=[
vm.Filter(column="Company"),
vm.Filter(column="Position"),
vm.Filter(column="Year", selector=vm.RangeSlider(step=1, marks=None, value=[2020, 2024])),
vm.Parameter(
targets=["role_chart_id.top_n", "company_chart_id.top_n"],
selector=vm.Slider(min=1, max=50, step=1, marks={1: "1", 10: "10", 20: "20", 30: "30", 40: "40", 50: "50"}, value=20)
)
],
)
dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()