File size: 8,203 Bytes
54df6fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba79e6b
54df6fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184a0b8
 
85a24c4
184a0b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54df6fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22ee7d3
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import streamlit as st
import json
import base64
import requests
from io import StringIO
from streamlit_agraph import agraph, Node, Edge, Config

st.title('Json File Reader')

@st.cache_data
def get_json(url):
    js = requests.get(url) 
    data = js.json()
    return data

st.markdown("""Reads the Json file of Comments data extracted from Youtube API & creates graph""")
st.sidebar.header('File Upload')
your_file = st.sidebar.file_uploader(label="Upload the file here")

if your_file is not None: 
    bytes_data = your_file.getvalue()

    json_data = json.loads(bytes_data)

else:
    st.write("Reference: https://blog.streamlit.io/the-streamlit-agraph-component/")

    json_data = get_json("https://raw.githubusercontent.com/insightbuilder/python_de_learners_data/main/code_script_notebooks/python_scripts/json_reader/toplevel_comment_zGAkhN1YZXM.json")
try:
        length = len(json_data)
        if length < 15:
            indices = st.sidebar.slider("Start n End",0,length,(0,10))
        else:
            indices = st.sidebar.slider("Start n End",0,length,(0,int(length/15)))
        
        selected_indices = json_data[indices[0]:indices[1]] 
        #st.write(selected_indices)
        #creating the graph of the connection

        nodes = []
        edges = []
        authors = []

        video_id = selected_indices[0]['snippet']['videoId']


        nodes.append(Node(id=video_id,lable='Youtube Video',
                          size = 25, symbolType='square'))
        
        for data in selected_indices:
            author = data['snippet']['topLevelComment']['snippet']['authorDisplayName'].split(' ')[0] 
            author_img = data['snippet']['topLevelComment']['snippet']['authorProfileImageUrl'] 
            if author not in authors:
                nodes.append(Node(id=author, 
                                  size=25, 
                                  shape="circularImage",
                                  image=author_img) )
                authors.append(author)
            if 'replies' in data:
                replies = data['replies']['comments']
                for reply in replies:
                    reply_author = reply['snippet']['authorDisplayName'].split(' ')[0]
                    reply_author_img = reply['snippet']['authorProfileImageUrl']
                    if reply_author not in authors:
                        nodes.append(Node(id=reply_author, 
                                          size=15, 
                                          shape="circularImage", 
                                          image=reply_author_img) )
                        authors.append(reply_author)

                    edges.append( Edge(source=reply_author, 
                                       target=author, 
                                       type="CURVE_SMOOTH"))

            edges.append(Edge(source=author, target=video_id,type="CURVE_SMOOTH"))

        #st.write(authors)

        config = Config(width=750,
                    height=950,
                    directed=True,
                    physics=False,
                    hierarchical=False,
                    node={'labelProperty':'label','renderLabel':True})

        return_value = agraph(nodes = nodes, edges = edges, config = config)

except Exception as e:
    st.write(e)
    st.markdown("Provided Json is not Youtube API data. Unable to Parse")
    #st.write(json_data)



"""

import streamlit as st
from py2neo import Graph, Node, Relationship
from scripts.viz import draw

# Initialize a Neo4j graph instance
graph = Graph()
graph.delete_all()

# Create nodes and relationships
nicole = Node("Person", name="Nicole", age=24)
drew = Node("Person", name="Drew", age=20)
mtdew = Node("Drink", name="Mountain Dew", calories=9000)
cokezero = Node("Drink", name="Coke Zero", calories=0)
coke = Node("Manufacturer", name="Coca Cola")
pepsi = Node("Manufacturer", name="Pepsi")

graph.create(nicole | drew | mtdew | cokezero | coke | pepsi)
graph.create(Relationship(nicole, "LIKES", cokezero))
graph.create(Relationship(nicole, "LIKES", mtdew))
graph.create(Relationship(drew, "LIKES", mtdew))
graph.create(Relationship(coke, "MAKES", cokezero))
graph.create(Relationship(pepsi, "MAKES", mtdew))

# Streamlit interface
st.title("Py2neo Application with Streamlit")

# Display graph visualization using Py2neo's draw function
st.subheader("Graph Visualization")
options = {"Person": "name", "Drink": "name", "Manufacturer": "name"}
draw(graph, options)

# Display node and relationship information
st.subheader("Node and Relationship Information")
st.write("Nodes:")
for node in [nicole, drew, mtdew, cokezero, coke, pepsi]:
    st.write(f"{node.labels}: {node}")

st.write("Relationships:")
for relationship in graph.match(rel_type="LIKES"):
    st.write(relationship)

st.write("Manufacturers:")
for manufacturer in graph.match(rel_type="MAKES"):
    st.write(manufacturer)

import streamlit as st
import json
import base64
import requests
from io import StringIO
from streamlit_agraph import agraph, Node, Edge, Config

st.title('Json File Reader')

@st.cache_data
def get_json(url):
    js = requests.get(url) 
    data = js.json()
    return data

st.sidebar.header('File Upload')
your_file = st.sidebar.file_uploader(label="Upload the file here")

if your_file is not None: 
    bytes_data = your_file.getvalue()

    json_data = json.loads(bytes_data)

else:
    st.write("Example api file can be located here")

    json_data = get_json("https://raw.githubusercontent.com/insightbuilder/python_de_learners_data/main/code_script_notebooks/python_scripts/json_reader/toplevel_comment_zGAkhN1YZXM.json")
try:
        length = len(json_data)
        if length < 15:
            indices = st.sidebar.slider("Start n End",0,length,(0,10))
        else:
            indices = st.sidebar.slider("Start n End",0,length,(0,int(length/15)))
        
        selected_indices = json_data[indices[0]:indices[1]] 
        #st.write(selected_indices)
        #creating the graph of the connection

        nodes = []
        edges = []
        authors = []

        video_id = selected_indices[0]['snippet']['videoId']


        nodes.append(Node(id=video_id,lable='Youtube Video',
                          size = 25, symbolType='square'))
        
        for data in selected_indices:
            author = data['snippet']['topLevelComment']['snippet']['authorDisplayName'].split(' ')[0] 
            author_img = data['snippet']['topLevelComment']['snippet']['authorProfileImageUrl'] 
            if author not in authors:
                nodes.append(Node(id=author, 
                                  size=25, 
                                  shape="circularImage",
                                  image=author_img) )
                authors.append(author)
            if 'replies' in data:
                replies = data['replies']['comments']
                for reply in replies:
                    reply_author = reply['snippet']['authorDisplayName'].split(' ')[0]
                    reply_author_img = reply['snippet']['authorProfileImageUrl']
                    if reply_author not in authors:
                        nodes.append(Node(id=reply_author, 
                                          size=15, 
                                          shape="circularImage", 
                                          image=reply_author_img) )
                        authors.append(reply_author)

                    edges.append( Edge(source=reply_author, 
                                       target=author, 
                                       type="CURVE_SMOOTH"))

            edges.append(Edge(source=author, target=video_id,type="CURVE_SMOOTH"))

        #st.write(authors)

        config = Config(width=750,
                    height=950,
                    directed=True,
                    physics=False,
                    hierarchical=False,
                    node={'labelProperty':'label','renderLabel':True})

        return_value = agraph(nodes = nodes, edges = edges, config = config)

except Exception as e:
    st.write(e)
    st.markdown("Provided Json is not Youtube API data. Unable to Parse")
    #st.write(json_data)

"""