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

Add initial state flag #519

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@

"cache_arguments" : True, # True for cache, False for session-based argument propagation

#"serve_locally" : True, # True to serve assets locally, False to use their unadulterated urls (eg a CDN)
"serve_locally" : True, # True to serve assets locally, False to use their unadulterated urls (eg a CDN)

"stateless_loader" : "demo.scaffold.stateless_app_loader",
}
Expand Down
2 changes: 1 addition & 1 deletion django_plotly_dash/dash_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def get_asset_static_url(self, asset_path):
module_name = self.caller_module.__name__
return static_asset_path(module_name, asset_path)

def as_dash_instance(self, cache_id=None):
def as_dash_instance(self, cache_id=None, can_ignore_initial_state=False):
'''
Form a dash instance, for stateless use of this app
'''
Expand Down
11 changes: 7 additions & 4 deletions django_plotly_dash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ def current_state(self):
setattr(self, '_current_state_hydrated_changed', False)
return c_state

def as_dash_instance(self, cache_id=None):
def as_dash_instance(self, cache_id=None, can_ignore_initial_state=False):
'Return a dash application instance for this model instance'
dash_app = self.stateless_app.as_dash_app() # pylint: disable=no-member
base = self.current_state()
if can_ignore_initial_state:
base = None
else:
base = self.current_state()
return dash_app.do_form_dash_instance(replacements=base,
specific_identifier=self.slug,
cache_id=cache_id)
Expand Down Expand Up @@ -220,15 +223,15 @@ def populate_values(self):
self.base_state = json.dumps(obj)

@staticmethod
def locate_item(ident, stateless=False, cache_id=None):
def locate_item(ident, stateless=False, cache_id=None, can_ignore_initial_state=False):
'''Locate a dash application, given either the
slug of an instance or the name for a stateless app'''
if stateless:
dash_app = find_stateless_by_name(ident)
else:
dash_app = get_object_or_404(DashApp, slug=ident)

app = dash_app.as_dash_instance(cache_id=cache_id)
app = dash_app.as_dash_instance(cache_id=cache_id, can_ignore_initial_state=can_ignore_initial_state)
return dash_app, app

class DashAppAdmin(admin.ModelAdmin):
Expand Down
3 changes: 2 additions & 1 deletion django_plotly_dash/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ def test_local_serving(settings):
'Test local serve settings'

from django_plotly_dash.util import serve_locally, static_asset_root, full_asset_path
assert serve_locally() == settings.DEBUG
# Now default to serving all files locally
assert serve_locally() == True
assert static_asset_root() == 'dpd/assets'
assert full_asset_path('fred.jim', 'harry') == 'dpd/assets/fred/jim/harry'

Expand Down
3 changes: 1 addition & 2 deletions django_plotly_dash/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def update(request, ident, stateless=False, **kwargs):

def _update(request, ident, stateless=False, **kwargs):
'Generate update json response'
dash_app, app = DashApp.locate_item(ident, stateless)
dash_app, app = DashApp.locate_item(ident, stateless, can_ignore_initial_state=True)

try:
request_body = json.loads(request.body.decode('utf-8'))
Expand Down Expand Up @@ -191,4 +191,3 @@ def add_stateless_apps(request, **kwargs):
"""Check all registered stateless apps and create ORM entries that are missing"""
check_stateless_loaded()
return redirect('admin:django_plotly_dash_statelessapp_changelist')