Click here to Skip to main content
15,844,602 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given code is not displaying graph trees properly.

What I have tried:

import json
import networkx as nx
import dash
from dash import dcc, html  # Update import statements
from dash.dependencies import Input, Output
import plotly.graph_objs as go

# Load the JSON formatted output
with open("structured_output.json", "r") as json_file:
    structured_output = json.load(json_file)

# Create a directed graph for positive bigrams
positive_tree = nx.DiGraph()
positive_tree.add_node("Positive Bigrams")

# Add subnodes (top 5 positive bigrams)
for entry in structured_output:
    positive_bigram = " ".join(entry["bigram"])  # Replace with the correct key for positive bigrams
    positive_tree.add_edge("Positive Bigrams", positive_bigram)

# Create a directed graph for negative bigrams
negative_tree = nx.DiGraph()
negative_tree.add_node("Negative Bigrams")

# Add subnodes (top 5 negative bigrams)
for entry in structured_output:
    negative_bigram = " ".join(entry["bigram"])  # Replace with the correct key for negative bigrams
    negative_tree.add_edge("Negative Bigrams", negative_bigram)

# Create Dash app
app = dash.Dash(__name__)

# Define app layout
app.layout = html.Div([
    html.H1("Bigrams Visualization"),
        dcc.Graph(id='positive-tree', config={'displayModeBar': False}),
        dcc.Graph(id='negative-tree', config={'displayModeBar': False})

# Callback to update the graphs
    [Output('positive-tree', 'figure'), Output('negative-tree', 'figure')],
    [Input('positive-tree', 'relayoutData'), Input('negative-tree', 'relayoutData')]
def update_graphs(positive_relayoutData, negative_relayoutData):
    pos_fig = go.Figure()
    pos_pos = nx.spring_layout(positive_tree)
        x=[pos_pos[node][0] for node in pos_pos],
        y=[pos_pos[node][1] for node in pos_pos],
        text=[node for node in pos_pos],
        textposition='bottom center'
    pos_fig.update_layout(title='Positive Bigrams Tree')

    neg_fig = go.Figure()
    neg_pos = nx.spring_layout(negative_tree)
        x=[neg_pos[node][0] for node in neg_pos],
        y=[neg_pos[node][1] for node in neg_pos],
        text=[node for node in neg_pos],
        textposition='bottom center'
    neg_fig.update_layout(title='Negative Bigrams Tree')

    return pos_fig, neg_fig

# Run the app
if __name__ == '__main__':
Updated 24-Aug-23 3:39am
Member 15627495 24-Aug-23 8:21am    
you are at the point where your code have to be debug.
you have to check :
- all vars Type, when input of a function. that is to avoid 'Type mismatch'
- all values, if they are 'empty' or 'filled'.

for the logical of your global code.
- do you use plotly the right way ?

to achieve, you have to "trace your vars" , and check what is in, at every moment required.

do you know where your code is laking ?
Andre Oosthuizen 24-Aug-23 8:41am    
You will have to be more specific - Quote: code is not displaying graph trees properly. Properly as in not at all, skew, red instead of blue etc etc.
Dave Kreskowiak 24-Aug-23 9:49am    
"not displaying graph trees properly." is not a problem description. You have to explain what "properly" means, and what is actually happening. Nobody can guess what you mean by this.

1 solution

"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

In this case, we can't even begin to help you as we have no access to your JSON input, and no idea what it should dis0lay. And since we can't run your code and see without the input data, there isn;t anythign we can do to help you.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger: pdb — The Python Debugger — Python 3.11.4 documentation[^]

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900