✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Not able to generate multiple output

I am trying to build a dashboard where one input will be from the user and another input will be selected from the dropdown and then my function will calculate the cosine similarity score of the user input with all the values of the dropdown. I then want to create a bar graph of the the scores to see which element from the dropdown has the highest similarity score with the user input.
I am not getting the score on dashboard for all elements, only first result is coming.
Can anyone please help me with this as I am new to plotly and not getting exactly how to fix this. The function I have written to calculate similarity score is working fine as standalone but with plotly not getting desired output.
Please see the code below
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from collections import Counter
from math import sqrt
import pandas as pd
import plotly.graph_objs as go

results = [‘immunity’,‘antioxidants’,‘health’]
def word2vec(word):
cw = Counter(word)
sw = set(cw)
lw = sqrt(sum(c * c for c in cw.values()))
return cw, sw, lw

def cosdis(v1, v2):
common = v1[1].intersection(v2[1])
return sum(v1[0][ch] * v2[0][ch] for ch in common) / v1[2] / v2[2]

app = dash.Dash(name)
app.layout = html.Div(
[
dcc.Input(id=“trend”, type=“text”, placeholder=“Enter Trend”,debounce=True,value=’’),
dcc.Dropdown(id=“dropdown”,options = [{“label”: i, “value”: i} for i in results],value=“All tags”),
#dcc.Input(id=“score”, type=“text”, placeholder=""),
html.Button(id=‘submit-button’, n_clicks=0, children=‘Score’),
dcc.Graph(id=‘bar_chart’),
html.Hr(),
html.Div(id=“out”),
])

@app.callback(
Output(“bar_chart”, “figure”),
[Input(“trend”, “value”),
Input(“dropdown”, “value”),
Input(“submit-button”,“n_clicks”)])
def number_render(key1, key2, res):
score =
col_data =
trend_data =
for key in results:
for word in key1:
res = cosdis(word2vec(word), word2vec(key))
score.append(res)
col_data.append(key)
trend_data.append(word)
bar_data = go.Bar(x=results,y=score)
layout = go.Layout(title=‘Similarity Score’)
fig = go.Figure(data=bar_data,layout=layout)
#return “The cosine similarity between : {} and : {} is: {}”.format(key1, key2, res)
return fig

if name == “main”:
app.run_server(debug=True)

Hello
I tried to reformat your code to be more lisible, did I made mistake in the conversion ?

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from collections import Counter
from math import sqrt
import pandas as pd
import plotly.graph_objs as go

results = ['immunity','antioxidants','health']

def word2vec(word):
    cw = Counter(word)
    sw = set(cw)
    lw = sqrt(sum(c * c for c in cw.values()))
    return cw, sw, lw

def cosdis(v1, v2):
    common = v1[1].intersection(v2[1])
    return sum(v1[0][ch] * v2[0][ch] for ch in common) / v1[2] / v2[2]

app = dash.Dash(__name__)
app.layout = html.Div(
    [
        dcc.Input(id="trend", type="text", placeholder="Enter Trend",debounce=True,value=''),
        dcc.Dropdown(id="dropdown",options = [{"label": i, "value": i} for i in results],value="All tags"),
        #dcc.Input(id="score", type="text", placeholder=""),
        html.Button(id='submit-button', n_clicks=0, children='Score'),
        dcc.Graph(id='bar_chart'),
        html.Hr(),
        html.Div(id="out"),
    ])

@app.callback(
    Output("bar_chart", "figure"),
    [Input("trend", "value"),
    Input("dropdown", "value"),
    Input("submit-button","n_clicks")])
def number_render(key1, key2, res):
    score = []
    col_data = []
    trend_data = [] 
    for key in results:
        for word in key1:
            res = cosdis(word2vec(word), word2vec(key))
            score.append(res)
            col_data.append(key)
            trend_data.append(word)
    bar_data = go.Bar(x=results,y=score)
    layout = go.Layout(title='Similarity Score')
    fig = go.Figure(data=bar_data,layout=layout)
    #return "The cosine similarity between : {} and : {} is: {}".format(key1, key2, res)
    return fig

if __name__ == "__main__":
    app.run_server(debug=True)

I see one first problem, you define key2, res as arguments for the number_render function and inside the function you do not use them, it may be the problem.

I hope it helps