File size: 3,371 Bytes
7349b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
# -*- coding: utf-8 -*-
"""GetMFData.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/github/Krishna-Kumar-Sankaran-Kutty/mfCompare/blob/main/GetMFData.ipynb

# Analyze MF Portfolio data from Money Control
"""

#Import Dependencies
import gradio as gr
from bs4 import BeautifulSoup
import requests
import pandas as pd
import plotly.express as px

# Define function to extract portfolio details from given url from Moneycontrol Portfolio Holdings page
def extract_portfolio(url):
  # Send an HTTP GET request to the URL
  response = requests.get(url)

  # Parse the HTML content using BeautifulSoup
  soup = BeautifulSoup(response.content, 'html.parser')

  # Find the table element
  table = soup.find('table', id='equityCompleteHoldingTable')

  # Extract table headers
  headers = [header.text.strip() for header in table.find_all('th')]

  # Extract table rows
  rows = []
  for row in table.find_all('tr')[1:]:  # Skip the header row
    data = [cell.text.strip() for cell in row.find_all('td')]
    rows.append(data)

  # Create a Pandas DataFrame
  df = pd.DataFrame(rows, columns=headers)

  # Add Fund Name column
  df['Fund Name'] = url.split('/')[-3].replace('-', ' ').title()

  # Set Fund Name column as index
  #df = df.set_index('Fund Name')

  # Clean-up 'Stock Invested in' column
  df['Stock Invested in'] = df['Stock Invested in'].str.replace('#\n', '')

  # Clean-up '% of Total Holdings' column nad convert to float
  df['% of Total Holdings'] = df['% of Total Holdings'].str.rstrip('%').astype(float)

  # Drop all columns except 'Stock Invested in', 'Sector Total', and '% of Total Holdings'
  df = df[['Fund Name', 'Sector', 'Stock Invested in', '% of Total Holdings']]

  return df

# Define function to generate treemap based on selected funds
def generate_treemap(fund1, fund2, fund3):
  fund_urls = {
      "Hdfc Index Fund Nifty 50 Plan Direct Plan": 'https://www.moneycontrol.com/mutual-funds/hdfc-index-fund-nifty-50-plan-direct-plan/portfolio-holdings/MHD1152',
      "Parag Parikh Flexi Cap Fund Direct Plan": 'https://www.moneycontrol.com/mutual-funds/parag-parikh-flexi-cap-fund-direct-plan/portfolio-holdings/MPP002',
      "Hdfc Nifty Next 50 Index Fund Direct Plan": 'https://www.moneycontrol.com/mutual-funds/hdfc-nifty-next-50-index-fund-direct-plan/portfolio-holdings/MHD3484'
  }

  selected_funds = [fund1, fund2, fund3]
  dfs = []
  for fund in selected_funds:
    if fund:
      url = fund_urls[fund]
      df = extract_portfolio(url)
      dfs.append(df)

  if dfs:
    combined_df = pd.concat(dfs)
    fig = px.treemap(combined_df,
                    path=[px.Constant("All Funds"), 'Fund Name', 'Sector', 'Stock Invested in'],
                    values='% of Total Holdings',
                    color='Fund Name',
                    template='ggplot2')
    return fig
  else:
    return None

# Create Gradio interface
fund_choices = [
    "Hdfc Index Fund Nifty 50 Plan Direct Plan",
    "Parag Parikh Flexi Cap Fund Direct Plan",
    "Hdfc Nifty Next 50 Index Fund Direct Plan"
]

iface = gr.Interface(
    fn=generate_treemap,
    inputs=[
        gr.Dropdown(choices=fund_choices, label="Fund 1"),
        gr.Dropdown(choices=fund_choices, label="Fund 2"),
        gr.Dropdown(choices=fund_choices, label="Fund 3")
],
    outputs=gr.Plot()
)

iface.launch()