Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dash does not properly work with library called jupyter_dash [BUG] #1907

Closed
arani-mohammad opened this issue Jan 31, 2022 · 6 comments
Closed

Comments

@arani-mohammad
Copy link

Thank you for providing this fantastic library.
I had a problem dash/jupyter_dash that I copied below.

dash                      2.1.0
dash-bootstrap-components 1.0.2
dash-core-components      2.0.0
dash-html-components      2.0.0
dash-renderer             1.1.2
dash-table                5.0.0
jupyter-dash              0.4.0

AttributeError Traceback (most recent call last)
/var/folders/xv/d091l2wd693b381xlqk8r1500000gn/T/ipykernel_5375/385882412.py in
50
51 if name == "main":
---> 52 app.run_server(mode = "external", debug=True, port=8050)
53 # app.run_server(debug=True, mode='inline',host="0.0.0.0",port=8058)

~/opt/anaconda3/envs/DPWorld2_conda_install_only/lib/python3.8/site-packages/jupyter_dash/jupyter_app.py in run_server(self, mode, width, height, inline_exceptions, **kwargs)
229 else:
230 requests_pathname_prefix = '/'
--> 231 self.config.update({'requests_pathname_prefix': requests_pathname_prefix})
232
233 # Compute server_url url

~/opt/anaconda3/envs/DPWorld2_conda_install_only/lib/python3.8/site-packages/dash/_utils.py in update(self, other)
167 # Overrides dict.update() to use setitem above
168 for k, v in other.items():
--> 169 self[k] = v
170
171 # pylint: disable=inconsistent-return-statements

~/opt/anaconda3/envs/DPWorld2_conda_install_only/lib/python3.8/site-packages/dash/_utils.py in setitem(self, key, val)
156 def setitem(self, key, val):
157 if key in self.dict.get("_read_only", {}):
--> 158 raise AttributeError(self._read_only[key], key)
159
160 final_msg = self.dict.get("_final")

AttributeError: ('Read-only: can only be set in the Dash constructor or during init_app()', 'requests_pathname_prefix')

@kaburelabs
Copy link

Same error here

@cogerk
Copy link

cogerk commented Feb 10, 2022

I'm also experiencing this error in a jupyter notebook without jupyter_dash, here's my notebook (scrubbed domain name for privacy):

import dash
from dash import dcc
from dash import html
import pandas as pd
import plotly.express as px
import dash_bootstrap_components as dbc
#NOTE: reload the whole app code if altering the callbacks, else the UI can bug out
app_port = 88900



#flask is default if nothing set
import flask
server = flask.Flask(__name__)

app = dash.Dash(__name__, server=server, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server


#Demo App
#--------

# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df = pd.DataFrame({
    "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
    "Amount": [4, 1, 2, 2, 4, 5],
    "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})

fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group")

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for your data.
    '''),

    dcc.Graph(
        id='example-graph',
        figure=fig
    )
])
from multiprocessing import Process
from IPython.display import IFrame
    

class Dash_jhub_app:

    # instance attribute
    def __init__(self,app):
        self.app = app
    
    def _render_app(self):
        self.app.config.update({
            # remove the default of '/'
            'requests_pathname_prefix': f'{self.port}/'
        })
        
        self.app.run_server(port=self.port,debug=self.debug) 
        
    def start(self,port,debug=True):
        self.port = port
        self.debug = debug
        self.thread= Process(target=self._render_app) #args=(self.app,self.port)
        self.thread.start()
    
    def url(self,user,url='https://jhub.DOMAIN.local/user/'):
        self.user = user
        self.url = f'{url}{self.user}/proxy/{self.port}'
        print(f'visit app at: {self.url}')  
        
    def show(self,color="white" ):
        return IFrame(self.url, width='100%', height='600px',allowtransparency="true",style=f"background-color: {color};" ) 
        
    def stop(self):
        self.thread.terminate()
_app = Dash_jhub_app(app)
_app.start(app_port) 
_app.url(user='cogert')
visit app at: https://jhub.DOMAIN.local/user/cogert/proxy/88900


Process Process-1:
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/opt/conda/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/tmp/ipykernel_1494/2485764532.py", line 14, in _render_app
    'requests_pathname_prefix': f'{self.port}/'
  File "/opt/conda/lib/python3.7/site-packages/dash/_utils.py", line 169, in update
    self[k] = v
  File "/opt/conda/lib/python3.7/site-packages/dash/_utils.py", line 158, in __setitem__
    raise AttributeError(self._read_only[key], key)
AttributeError: ('Read-only: can only be set in the Dash constructor or during init_app()', 'requests_pathname_prefix')
! pip freeze | grep dash
dash==2.1.0
dash-bootstrap-components==1.0.2
dash-core-components==2.0.0
dash-html-components==2.0.0
dash-table==5.0.0
_app.stop()

@jamescw
Copy link

jamescw commented Feb 10, 2022

Same here and the jupyter_dash lib has not seen a commit in a long time!

@alexcjohnson
Copy link
Collaborator

jupyter_dash hasn't had a commit in a long time because it's a thin wrapper around regular dash - it does what it needs to do and updates to dash just work with it. Until this update 🙃

We're working on a fix, but in the meantime, add this before your app.run_server call and it'll work with Dash 2.1:

del app.config._read_only["requests_pathname_prefix"]

Main issue: plotly/jupyter-dash#75

@jamescw
Copy link

jamescw commented Feb 11, 2022

Great thanks @alexcjohnson!

@alexcjohnson
Copy link
Collaborator

Fixed in jupyter-dash==0.4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants