Hello! I’m doing a Dashboard and I want to release a new tab when I click on the scatterplot. I have already my code, but I don’t know how to give the link into a component that opens up this new tab. I tried by using html.Link and html.A
import dash
from dash import html
from dash import dcc
import pandas as pd
import plotly.express as px
from dash.dependencies import Input,Output
import dash_bootstrap_components as dbc
from scipy.stats import linregress
df = pd.read_csv('genres_v2.csv',sep=',')
df['duration_ms'] = df['duration_ms']*(0.001/60)
genre = df['genre'].unique()
app = dash.Dash(external_stylesheets=[dbc.themes.CYBORG])
app.layout = html.Div([
html.Img(src=app.get_asset_url('spotify.jpg'), #introduccion de imagen desde carpeta 'assets'
style={'height':'5%','width':'5%'}),
html.H3('Siente tu Música preferida', #tipo de Header(es más pequeño que H1)
style={'textAlign':'center','color':'#1DB954',
'font-family':'gotham','font-weight':'bold'}),
html.Br(),
html.Label('Características',
style={'color':'white','font-weight':'bold'}),
dcc.Dropdown(id='first_var', #escoger las variables que se van a correlacionar o no
options=[{'label':'Bailibilidad','value':'danceability'},
{'label':'Energía','value':'energy'},
{'label':'Sonoridad','value':'loudness'},
{'label':'Hablabilidad','value':'speechiness'},
{'label':'Acústica','value':'acousticness'},
{'label':'Instrumentalidad','value':'instrumentalness'},
{'label':'Viveza','value':'liveness'},
{'label':'Valencia','value':'valence'},
{'label':'Tempo','value':'tempo'}
],
placeholder='Seleccionar variables de interés',
multi = True,
searchable= True),
html.Br(),
html.A(html.Button('Reset',style={'background-color':'#1DB954'
,'color':'white',
'font-family':'gotham',
'height':'10%','width':'10%',
'font-weight':'bold'}),href='/'), #Refresh la página para nuevo dropdown
html.Br(),
html.Label('Duración de la canción [min]',
style={'color':'white','font-weight':'bold'}),
dcc.RangeSlider(id='duration_slide',
min=df['duration_ms'].min(),
max=df['duration_ms'].max(),
value=[2.5,5.5],
tooltip={"placement": "bottom", "always_visible": True}, #instead marks
step=0.5),
html.Br(),
html.Label('Escoger un Género',
style={'color':'white','font-weight':'bold'}),
dcc.RadioItems(id='my_genre',
options =[{'label':i ,'value':i} for i in genre],
style={'color':'#1DB954'},
labelStyle={'display': 'inline-block','width':'12%'}), #para poder darle formato al los bullets
html.Br(),
dcc.Graph(id='Graph_cor'),
dcc.Textarea(id='mytext',
style={'background-color':'black','color':'#1DB954','textAlign':'center',
'font-weight':'bold'},
disabled= True),
html.Link(id='my_link',target='_blank')
])
@app.callback(
Output(component_id='first_var', component_property='disabled'),
Input(component_id='first_var', component_property='value')
)
def disable_dropdown_one(val1): #permite solo dos inputs del dropdown
if len(val1) == 2:
return True
else:
return False
@app.callback(Output(component_id='Graph_cor',component_property='figure'),
Output(component_id='mytext',component_property='value'),
Input(component_id='first_var',component_property='value'),
Input(component_id='duration_slide',component_property='value'),
Input(component_id='my_genre',component_property='value')
)
def grafica_correlacion(variables,tiempo,genero):
df1 = df[df['duration_ms']>=tiempo[0]]
df1=df1[df1['duration_ms']<= tiempo[1]]
df1 = df1[df1['genre']==genero]
slope, intercept, r, p, stderr = linregress(df1[variables[0]],
df1[variables[1]])
texto = 'La correlación es de {}'.format(r)
fig = px.scatter(df1,x= variables[0],
y= variables[1],title = 'Correlación entre {} y {}'.format(variables[0],variables[1]),
color = 'mode',size='duration_ms',color_continuous_scale=px.colors.sequential.Greens,
hover_name='song_name',labels={'song_name':'Song'})
fig.update_layout(plot_bgcolor='black',
paper_bgcolor='black', font={'color':'white'})
fig.update_layout(transition_duration=500) #modificar y actualizar figura
return fig, texto
@app.callback(Output(component_id= 'my_link', component_property = 'href'),
Input(component_id= 'Graph_cor', component_property='clickData'))
def to_link (new_data):
#print(new_data)
list1 = new_data.get('points')
dict1 = list1[0]
songs_time = dict1.get('marker.size')
hola = df[df['duration_ms']==songs_time]
code = hola['id'].iloc[0] #select the code for a data frame
link = "https://open.spotify.com/track/" + code
return link
app.run_server()