Py.Cafe

stichbury/

linkedin-connections-prototype

LinkedIn Connection Insights

DocsPricing
  • app.py
  • connections.csv
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Vizro is an open-source toolkit for creating modular data visualization applications.
# check out https://github.com/mckinsey/vizro for more info about Vizro
# and checkout https://vizro.readthedocs.io/en/stable/ for documentation.

import vizro.plotly.express as px
from vizro import Vizro
import vizro.models as vm
from vizro.models.types import capture

import pandas as pd
import plotly.graph_objects as go
import vizro.plotly.express as px


df = pd.read_csv("connections.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 common_jobs(data_frame):
    # Count the frequency of each position
    position_counts = data_frame["Position"].value_counts().head(20)
    # Create a horizontal bar chart
    fig = px.bar(
        position_counts,
        x=position_counts.values,
        y=position_counts.index,
        orientation="h",
        title="Most Common Positions",
    )

    # Update layout for better readability
    fig.update_layout(
        yaxis_title="Position",
        xaxis_title="Frequency",
        yaxis=dict(categoryorder="total ascending"),
    )
    return fig

@capture("graph")
def common_companies(data_frame):
    # Count the occurrences of each company
    company_counts = data_frame["Company"].value_counts().nlargest(5)
    # Create a DataFrame for the top 5 companies
    top_companies = company_counts.reset_index()
    top_companies.columns = ["Company", "Count"]

    # Create a doughnut chart
    fig = px.pie(
        top_companies,
        values="Count",
        names="Company",
        hole=0.4,
        title="Most Common Companies",
    )
    return fig


@capture("graph")
def cumulative_totals(data_frame):
    # Convert 'Connected On' to datetime and extract the year
    data_frame["Year"] = pd.to_datetime(data_frame["Connected On"]).dt.year
    # Count connections per year
    yearly_counts = data_frame["Year"].value_counts().sort_index()
    # Calculate cumulative sum of connections
    cumulative_counts = yearly_counts.cumsum()
    # Create figure with secondary y-axis
    fig = go.Figure()

    # Add bar chart for yearly connections
    fig.add_trace(go.Bar(x=yearly_counts.index, y=yearly_counts, name="Connections per Year"))
    # Add scatter plot for cumulative connections
    fig.add_trace(
        go.Scatter(
            x=cumulative_counts.index,
            y=cumulative_counts,
            name="Cumulative Connections",
            yaxis="y2",
        )
    )

    # Set up the layout for secondary y-axis
    fig.update_layout(yaxis2=dict(title="Cumulative Total", overlaying="y", side="right"))
    return fig

page = vm.Page(
    title="My LinkedIn connections",
    layout=vm.Layout(grid=[[0, 1], [0, 2]]),
    components=[
        vm.Graph(id="role_bar_chart_id", figure=common_jobs(data_frame=df)),
        vm.Graph(id="company_pie_chart_id", figure=common_companies(data_frame=df)),
        vm.Graph(id="growth_cumulative_chart_id", figure=cumulative_totals(data_frame=df)),
    ],
    
)

dashboard = vm.Dashboard(pages=[page], theme="vizro_light")
Vizro().build(dashboard).run()