Black Lives Matter. Please consider donating to Black Girls Code today.

Add js in dashh app

I want simulate this in dash:

https://codepen.io/Op_Je_Plaatsman/pen/VdKEam

So i have mi code:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
import visdcc
import pandas as pd
import dash_dangerously_set_inner_html

external_scripts = []
external_stylesheets = []

app = dash.Dash(external_scripts = external_scripts,
                external_stylesheets = external_stylesheets)

app.layout = html.Div([
    html.Div([
        html.Div([
            html.Div([
                html.Div(className="ui-slider-range ui-corner-all ui-widget-header"),
                html.Span(
                    tabIndex = 0,
                    className='ui-slider-handle ui-corner-all ui-state-default'
                ),
                html.Span(
                    tabIndex = 3500,
                    className='ui-slider-handle ui-corner-all ui-state-default'
                )
            ],id="slider-range",className="ui-slider ui-corner-all ui-slider-horizontal ui-widget ui-widget-content"),
            html.Span("0",id="min-price", **{'data-currency':'€'},className='slider-price'),
            html.Span(className="seperator"),
            html.Span("3500",id="max-price", **{'data-ocurrency':'€','data-max':'3500'},className="slider-price")
        ],className="price-slider")
    ],className="selector"),
    
    html.Div(id="slider-range2")
   
])
       
if __name__ == '__main__':
    app.run_server()

The css:

.selector {
    position: relative;
    padding: 20px;
    width: 400px;
    color: #7e7e7e;
}

.selector ul {
    position: relative;
    display: block;
    overflow: auto;
    min-width: 138px;
    max-height: 200px;
    background: #fff;
    list-style: none;
    white-space: inherit;
    padding-right: 17px;
    width: calc(100% + 17px)
}

.selector li {
    position: relative;
    padding: 3px 20px 3px 25px;
    cursor: pointer
}

.selector li:before {
    position: absolute;
    top: 50%;
    left: 0;
    top: 4px;
    display: inline-block;
    margin-right: 9px;
    width: 17px;
    height: 17px;
    background-color: #f4f4f4;
    border: 1px solid #d5d5d5;
    content: ""
}

.selector li[data-selected="1"]:before {
    border: 1px solid #d7d7d7;
    background-color: #fff
}

.selector li[data-selected="1"]:after {
    position: absolute;
    top: 50%;
    left: 3px;
    top: 11px;
    display: inline-block;
    width: 4px;
    height: 10px;
    border-right: 2px solid;
    border-bottom: 2px solid;
    background: none;
    color: #39c9a9;
    content: "";
    -webkit-transform: rotate(40deg) translateY(-50%);
    transform: rotate(40deg) translateY(-50%)
}

.selector li:hover {
    color: #aaa
}

.selector li .total {
    position: absolute;
    right: 0;
    color: #d7d7d7
}

.selector .price-slider {
    text-align: center;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    position: relative;
    padding-top: 17px
}

@media (min-width: 768px) {
    .selector .price-slider {
        padding-top:8px
    }
}

.selector .price-slider:before {
    position: absolute;
    top: 50%;
    left: 0;
    margin-top: 0;
    color: #39c9a9;
    content: attr(data-currency);
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%)
}

.selector #slider-range {
    width: 90%;
    margin-bottom: 30px;
    border: none;
    background: #e2f7f2;
    height: 3px;
    margin-left: 8px;
    margin-right: 8px
}

@media (min-width: 768px) {
     .selector #slider-range {
        width:100%
    }
}

.selector .ui-slider-handle {
    border-radius: 50%;
    background-color: #39c9a9;
    border: none;
    top: -14px;
    width: 28px;
    height: 28px;
    outline: none
}

@media (min-width: 768px) {
    .selector .ui-slider-handle {
        top:-7px;
        width: 16px;
        height: 16px
    }
}

.selector .ui-slider-range {
    background-color: #d7d7d7
}

.selector .slider-price {
    position: relative;
    display: inline-block;
    padding: 5px 40px;
    width: 40%;
    background-color: #e2f7f2;
    line-height: 28px;
    text-align: center
}

.selector .slider-price:before {
    position: absolute;
    top: 50%;
    left: 13px;
    margin-top: 0;
    color: #39c9a9;
    content: attr(data-currency);
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%)
}

.selector .show-all {
    position: relative;
    padding-left: 25px;
    color: #39c9a9;
    cursor: pointer;
    line-height: 28px
}

.selector .show-all:after, .selector .show-all:before {
    content: "";
    position: absolute;
    top: 50%;
    left: 4px;
    margin-top: -1px;
    color: #39c9a9;
    width: 10px;
    border-bottom: 1px solid
}

.selector .show-all:after {
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg)
}

.selector.open ul {
    max-height: none
}

.selector.open .show-all:after {
    display: none
}


* {
    -webkit-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
}

and that’s fine, but when I add the js in the asset file nothing happens

$("#slider-range").slider({
  range: true, 
  min: 0,
  max: 3500,
  step: 50,
  slide: function( event, ui ) {
    $( "#min-price").html(ui.values[ 0 ]);
    
    suffix = '';
    if (ui.values[ 1 ] == $( "#max-price").data('max') ){
       suffix = ' +';
    }
    $( "#max-price").html(ui.values[ 1 ] + suffix);         
  }
})

@JoseMarqueses Cool idea! Do you have JQuery loaded as well? You need that library to use its associated $ functions. You can either download the library (I would suggest a minified version) or link directly to a hosted version [e.g. on a Content Delivery Network (CDN)] in your external_scripts list.

https://code.jquery.com/

If you check the “Settings” of that CodePen, you’ll see that JQuery is listed as a (in fact, the only) dependency:

image

hi @wbrgss, I added this:

external_scripts = ['https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js',
                    'https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js',]
external_stylesheets = ['https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css']

app = dash.Dash(external_scripts = external_scripts,
                external_stylesheets = external_stylesheets)

but doesnt work!

@JoseMarqueses OK, looks like you have the right JQuery dependencies now. I tried your app out, and now I see another reason it’s not working. I logged an issue here: https://github.com/plotly/dash/issues/1041

In the meantime, you should be able to hack around this with the following JS, which polls for document.readyState:

var interval = setInterval(function(){
   if(document.readyState === 'complete') {
       initSlider();
   }
}, 2); // 2 = 2ms, higher value = less frequent polling but better perf

var initSlider = function() {
	$("#slider-range").slider({
	  range: true, 
	  min: 0,
	  max: 3500,
	  step: 50,
	  slide: function( event, ui ) {
	    $( "#min-price").html(ui.values[ 0 ]);

	    suffix = '';
	    if (ui.values[ 1 ] == $( "#max-price").data('max') ){
	       suffix = ' +';
	    }
	    $( "#max-price").html(ui.values[ 1 ] + suffix);         
	  }
	})

	clearInterval(interval);
}

that works for you? no for me :sob:

EDIT:

Works!!! Only I had to put "_ name _ " in:

app = dash.Dash( __name__ ,external_scripts = external_scripts,
                external_stylesheets = external_stylesheets)