Skip to content

Commit b3b9de9

Browse files
authored
Merge pull request #983 from plotly/dbg-r-reload
Dbg r reload
2 parents 8e0c80d + 74be160 commit b3b9de9

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to `dash` will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## Unreleased
6+
### Fixed
7+
- [#983](https://github.com/plotly/dash/pull/983) Fix the assets loading issues when dashR application runner is handling with an app defined by string chunk.
8+
59
## [1.5.1] - 2019-10-29
610
### Fixed
711
- [#987](https://github.com/plotly/dash/pull/987) Fix cache string handling for component suites with nested folders in their packages.

dash/testing/application_runners.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import uuid
66
import shlex
77
import threading
8+
import shutil
89
import subprocess
910
import logging
1011
import inspect
@@ -61,6 +62,7 @@ def __init__(self, keep_open, stop_timeout):
6162
self.started = None
6263
self.keep_open = keep_open
6364
self.stop_timeout = stop_timeout
65+
self._tmp_app_path = None
6466

6567
def start(self, *args, **kwargs):
6668
raise NotImplementedError # pragma: no cover
@@ -103,6 +105,10 @@ def url(self):
103105
def is_windows(self):
104106
return sys.platform == "win32"
105107

108+
@property
109+
def tmp_app_path(self):
110+
return self._tmp_app_path
111+
106112

107113
class ThreadedRunner(BaseDashRunner):
108114
"""Runs a dash application in a thread.
@@ -261,13 +267,18 @@ def start(self, app, start_timeout=2, cwd=None):
261267
cwd = os.path.dirname(app)
262268
logger.info("RRunner inferred cwd from app path: %s", cwd)
263269
else:
264-
path = (
265-
"/tmp/app_{}.R".format(uuid.uuid4().hex)
266-
if not self.is_windows
267-
else os.path.join(
268-
(os.getenv("TEMP"), "app_{}.R".format(uuid.uuid4().hex))
269-
)
270+
self._tmp_app_path = os.path.join(
271+
"/tmp" if not self.is_windows else os.getenv("TEMP"),
272+
uuid.uuid4().hex,
270273
)
274+
try:
275+
os.mkdir(self.tmp_app_path)
276+
except OSError:
277+
logger.exception(
278+
"cannot make temporary folder %s", self.tmp_app_path
279+
)
280+
path = os.path.join(self.tmp_app_path, "app.R")
281+
271282
logger.info("RRunner start => app is R code chunk")
272283
logger.info("make a temporary R file for execution => %s", path)
273284
logger.debug("content of the dashR app")
@@ -283,11 +294,11 @@ def start(self, app, start_timeout=2, cwd=None):
283294
for entry in inspect.stack():
284295
if "/dash/testing/" not in entry[1].replace("\\", "/"):
285296
cwd = os.path.dirname(os.path.realpath(entry[1]))
297+
logger.warning("get cwd from inspect => %s", cwd)
286298
break
287299
if cwd:
288300
logger.info(
289-
"RRunner inferred cwd from the Python call stack: %s",
290-
cwd
301+
"RRunner inferred cwd from the Python call stack: %s", cwd
291302
)
292303
else:
293304
logger.warning(
@@ -297,16 +308,40 @@ def start(self, app, start_timeout=2, cwd=None):
297308
"dashr.run_server(app, cwd=os.path.dirname(__file__))"
298309
)
299310

311+
# try copying all valid sub folders (i.e. assets) in cwd to tmp
312+
# note that the R assets folder name can be any valid folder name
313+
assets = [
314+
os.path.join(cwd, _)
315+
for _ in os.listdir(cwd)
316+
if not _.startswith("__")
317+
and os.path.isdir(os.path.join(cwd, _))
318+
]
319+
320+
for asset in assets:
321+
target = os.path.join(
322+
self.tmp_app_path, os.path.basename(asset)
323+
)
324+
if os.path.exists(target):
325+
logger.debug("delete existing target %s", target)
326+
shutil.rmtree(target)
327+
logger.debug("copying %s => %s", asset, self.tmp_app_path)
328+
shutil.copytree(asset, target)
329+
logger.debug("copied with %s", os.listdir(target))
330+
300331
logger.info("Run dashR app with Rscript => %s", app)
332+
301333
args = shlex.split(
302-
"Rscript {}".format(os.path.realpath(app)),
334+
"Rscript -e 'source(\"{}\")'".format(os.path.realpath(app)),
303335
posix=not self.is_windows,
304336
)
305337
logger.debug("start dash process with %s", args)
306338

307339
try:
308340
self.proc = subprocess.Popen(
309-
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd
341+
args,
342+
stdout=subprocess.PIPE,
343+
stderr=subprocess.PIPE,
344+
cwd=self.tmp_app_path if self.tmp_app_path else cwd,
310345
)
311346
# wait until server is able to answer http request
312347
wait.until(

0 commit comments

Comments
 (0)