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

Pr240 fixing static directory creation issue #242

Prev Previous commit
Next Next commit
Included function to copy static files into Scene._generate_json
  • Loading branch information
sahilshekhawat committed Jun 18, 2015
commit 640d7a574a180f1c4259c98ba268d171edfc1edb
68 changes: 24 additions & 44 deletions pydy/viz/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def __init__(self, reference_frame, origin, *visualization_frames,

Notes
=====
The user is allow to supply either system or time, constants,
The user is allowed to supply either system or time, constants,
states_symbols, and states_trajectories. Providing a System allows for
interactively changing the simulation parameters via the Scene GUI
in the IPython notebook.
Expand Down Expand Up @@ -136,7 +136,7 @@ def __init__(self, reference_frame, origin, *visualization_frames,
for k, v in default_kwargs.items():
setattr(self, k, v)

self.static_url = os.path.join(os.getcwd(), "pydy-resources")
self._static_url = os.path.join(os.getcwd(), "pydy-resources")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that this is the best place to create the path to this dir. The user coudl theorectically change directory in between the calls to __init__() and display().

I would make a class level attribute called pydy_dir or something. And then in the display() methods you should do this os.path.join.


@property
def name(self):
Expand Down Expand Up @@ -204,11 +204,11 @@ def system(self, new_system):
self._system = new_system

@property
def time(self):
return self._time
def times(self):
return self._times

@time.setter
def time(self, new_time):
@times.setter
def times(self, new_time):

try:
if new_time is not None and self.system is not None:
Expand All @@ -230,9 +230,9 @@ def time(self, new_time):
pass

if new_time is None:
self._time = new_time
self._times = new_time
else:
self._time = np.array(new_time)
self._times = np.array(new_time)

@property
def states_symbols(self):
Expand Down Expand Up @@ -360,17 +360,25 @@ def generate_visualization_json(self, dynamic_variables,
self._generate_json(prefix=outfile_prefix)

def _generate_json(self, directory=None, prefix=None):
"""Creates two JSON files in the specified directory. One contains
the scene information and one contains the simulation data. If
"""Creates two JSON files and copies all the necessary static
files in the specified directory . One of the JSON files contains
the scene information and other one contains the simulation data. If
``directory`` is None the files will be created in the current
working directory."""
working directory.
"""

if directory is None:
directory = os.getcwd()
directory = self._static_url

if prefix is None:
prefix = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')

if os.path.exists(directory):
distutils.dir_util.remove_tree(directory)

src = os.path.join(os.path.dirname(__file__), 'static')
distutils.dir_util.copy_tree(src, directory)

self._scene_json_file = prefix + "_scene_desc.json"
self._simulation_json_file = prefix + "_simulation_data.json"

Expand Down Expand Up @@ -595,38 +603,10 @@ def remove_static_html(self, force=False):
else:
print("aborted!")

def copy_resources(self, silent=False):
"""
Copies simulation and static files to a directory pointed
by self.static_url attribute.
It overwrites if such directory is already present.
"""
dst = self.static_url
if os.path.exists(dst):
if not silent:
print("Cleaning previous static data")
distutils.dir_util.remove_tree(dst)

src = os.path.join(os.path.dirname(__file__), 'static')

if not silent:
print("Copying static data.")
distutils.dir_util.copy_tree(src, dst)

if not silent:
print("Copying Simulation data.")
try:
shutil.move(os.path.join(os.getcwd(), self._scene_json_file),
os.path.join(self.static_url, self._scene_json_file))
shutil.move(os.path.join(os.getcwd(), self._simulation_json_file),
os.path.join(self.static_url, self._simulation_json_file))
except IOError:
pass

def display(self):
"""Displays the scene in the default webbrowser."""
self.copy_resources()
server = Server(scene_file=self._scene_json_file)
self._generate_json()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you have to supply the directory kwarg here, other wise the json files will not be copied into the _static_url directory.

server = Server(scene_file=self._scene_json_file, directory=self._static_url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't clear to me when self._static_url is created.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it in __init__, see my note there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why is this called a url?

server.run_server()

def _rerun_button_callback(self, btn):
Expand Down Expand Up @@ -710,7 +690,7 @@ def display_ipython(self):
'IPython >= 3.0. Please update IPython and try again.')
raise ImportError(msg.format(IPython.__version__))

self.copy_resources(silent=True)
self._generate_json()

# Only create the constants input boxes and the rerun simulation
# button if the scene was generated with a System.
Expand All @@ -732,7 +712,7 @@ def display_ipython(self):
display(self._constants_container)
display(self._rerun_button)

ipython_static_url = os.path.relpath(self.static_url, os.getcwd())
ipython_static_url = os.path.relpath(self._static_url, os.getcwd())

with open(os.path.join(ipython_static_url, "index_ipython.html"), 'r') as html_file:
html = html_file.read()
Expand Down
1 change: 1 addition & 0 deletions pydy/viz/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(self, scene_file, directory="static/", port=8000):
def run_server(self):
# Change dir to static first.
os.chdir(self.directory)
print(os.getcwd())
# Get a free port
while self._check_port(self.port):
self.port += 1
Expand Down
8 changes: 4 additions & 4 deletions pydy/viz/tests/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ def test_copy_resources(self):
outfile_prefix="test")
# test custom directory creation pointed by scene.static_url
scene.copy_resources()
assert os.path.exists(scene.static_url)
assert os.path.exists(os.path.join(scene.static_url, 'index.html'))
assert os.path.exists(os.path.join(scene.static_url, 'test_scene_desc.json'))
assert os.path.exists(os.path.join(scene.static_url, 'test_simulation_data.json'))
assert os.path.exists(scene._static_url)
assert os.path.exists(os.path.join(scene._static_url, 'index.html'))
assert os.path.exists(os.path.join(scene._static_url, 'test_scene_desc.json'))
assert os.path.exists(os.path.join(scene._static_url, 'test_simulation_data.json'))

def test_generate_visualization_json_system(self):

Expand Down