<jupyter_start><jupyter_text><jupyter_code>pip install jupyter-dash
import numpy as np
import pandas as pd 
from scipy import stats
from plotly.offline import iplot, init_notebook_mode
import plotly.graph_objs as go
import dash

#import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
#def configure_plotly_browser_state():
 # import IPython
  #display(IPython.core.display.HTML('''
  #<script src="/static/components/requirejs/require.js"></script>
  #<script>
  #requirejs.config({
   # paths: {
    #  base: '/static/base',
     # plotly: 'http://cdn.plot.ly/plotly-1.5.1.min.js?noext', 
   # },
  #});
  #</script>
  #'''))
dataset = pd.read_csv("/content/data.csv")
dataset

#Will probably need to make a new DF from the results of the model, fetching the elements we want to map
instrumentalness = ["0.000001", "0.000005", "0.000010", "0.000050", "0.000100", "0.000500", "0.001000", "0.005000", "0.010000", "0.050000", "0.100000", "0.500000",
         "1.000000"]
#Here we want to check the range of the popularity element and map it in increments in place of years. 
#Need to also make sure the columns we are using are formatted uniformily - ie. years seems to be in multiple formats but it might be the only one - clean up the dataset

#sub artists - this is a string and there will only be ten, they will be in the legend on the side
#figure out if we need to differentiate by using singular and plural forms (artist in artists) - it is put this way now:
#year = popularity, years = popularities, continent = artists, continents = artist

names = []
for name in dataset["name"]:
    if name not in names:
        names.append(name)
# make figure
fig_dict = {
    "data": [],
    "layout": {},
    "frames": []
}
# fill in most of layout
fig_dict["layout"]["xaxis"] = {"range": [100000, 900000], "title": "Duration"}
fig_dict["layout"]["yaxis"] = {"title": "Tempo", "type": "log"}
fig_dict["layout"]["hovermode"] = "closest"
fig_dict["layout"]["updatemenus"] = [
    {
        "buttons": [
            {
                "args": [None, {"frame": {"duration": 500, "redraw": False},
                                "fromcurrent": True, "transition": {"duration": 300,
                                                                    "easing": "quadratic-in-out"}}],
                "label": "Play",
                "method": "animate"
            },
            {
                "args": [[None], {"frame": {"duration": 0, "redraw": False},
                                  "mode": "immediate",
                                  "transition": {"duration": 0}}],
                "label": "Pause",
                "method": "animate"
            }
        ],
        "direction": "left",
        "pad": {"r": 10, "t": 87},
        "showactive": False,
        "type": "buttons",
        "x": 0.1,
        "xanchor": "right",
        "y": 0,
        "yanchor": "top"
    }
]

sliders_dict = {
    "active": 0,
    "yanchor": "top",
    "xanchor": "left",
    "currentvalue": {
        "font": {"size": 20},
        "prefix": "Popularity:",
        "visible": True,
        "xanchor": "right"
    },
    "transition": {"duration": 300, "easing": "cubic-in-out"},
    "pad": {"b": 10, "t": 50},
    "len": 0.9,
    "x": 0.1,
    "y": 0,
    "steps": []
}
# make data - switch instrumentalness and popularity
#switch name and artists
#sizemode may need to be changed from area if that is referring to actual land area, but that isn't a column in OG set so prob not?


instrumentalness = 0.500000
for name in names:
    dataset_by_pop = dataset[dataset["instrumentalness"] == instrumentalness]
    #dataset_by_pop_and_art = dataset_by_pop[dataset_by_pop["name"] == name]

    data_dict = {
        "x": list(dataset_by_pop["duration_ms"]),
        "y": list(dataset_by_pop["tempo"]),
        "mode": "markers",
        "text": list(dataset_by_pop["name"]),
        "marker": {
            "sizemode": "area",
            "sizeref": 200000,
            "size": list(dataset_by_pop["popularity"])
        },
        "name": name
    }
    fig_dict["data"].append(data_dict)
#dataset_by_pop_and_art = dataset_by_pop[dataset_by_pop["name"] == name]
dataset_by_pop

data_dict
#configure_plotly_browser_state()
#init_notebook_mode(connected=False)
# make frames
for instrumentalness in instrumentalness:
    frame = {"data": [], "name": ("instrumentalness")}
    for name in names:
        dataset_by_pop = dataset[dataset["instrumentalness"] == ("instrumentalness")]
        #dataset_by_pop_and_art = dataset_by_pop[
           # dataset_by_pop["name"] == name]

        data_dict = {
            "x": list(dataset_by_pop["duration_ms"]),
            "y": list(dataset_by_pop["tempo"]),
            "mode": "markers",
            "text": list(dataset_by_pop["name"]),
            "marker": {
                "sizemode": "area",
                "sizeref": 200000,
                "size": list(dataset_by_pop_and_art["speechiness"])
            },
            "name": name
        }
        frame["data"].append(data_dict)

    fig_dict["frames"].append(frame)
    slider_step = {"args": [
        [popularity],
        {"frame": {"duration": 300, "redraw": False},
         "mode": "immediate",
         "transition": {"duration": 300}}
    ],
        "label": popularity,
        "method": "animate"}
    sliders_dict["steps"].append(slider_step)


fig_dict["layout"]["sliders"] = [sliders_dict]

fig = go.Figure(fig_dict)

fig.show()
#iplot({"data":data,
 #      "layout":layout})

# iplot({fig})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15712762175613224, 'fraction_numerical': 0.02861713473160327, 'mean_word_length': 2.6350129198966408, 'pattern_counts': {'":': 87, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '260338', 'license': 'MIT', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1775, 'n_words': 465, 'path': 'Spotify_Plotly_DashApp.ipynb', 'repo_name': 'kvinne-anc/SpotifyApp-Project', 'size': '5626'}