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

Added create_static method to Scene class. #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
91 changes: 76 additions & 15 deletions pydy_viz/scene.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import json
import distutils
import distutils.dir_util
Expand Down Expand Up @@ -316,24 +317,84 @@ def generate_visualization_json(self, dynamic_variables,
separators=(',', ': ')))
outfile.close()

def _copy_static_dir(self):
"""
Copies all the static files required in the
visualization to the current working directory
The files and sub directories are stored within
a hidden directory named .pydy_viz in the current
working directory.
Working directory can be cleaned by calling _cleanup()
method, which deletes the .pydy_viz directory.
def create_static_html(self, overwrite=False):
"""Creates a directory named ``static`` in the current working
directory which contains all of the HTML, CSS, and Javascript files
required to run the visualization. Simply open ``static/index.html``
in a WebGL compliant browser to view and interact with the
visualization.

This method can also be used to output files for embedding the
visualizations in the static webpages. Simply copy the contents of
static directory in the relevant directory for embedding in a static
website.

Parameters
----------
overwrite : boolean, optional, default=False
If true, the directory named ``static`` in the current workind
directory will be overwritten.

"""
dst = os.path.join(os.getcwd(), '.pydy_viz')
os.mkdir(dst)
src = os.path.join(os.path.dirname(pydy_viz.__file__), 'static')
distutils.dir_util.copy_tree(src, dst)

def _cleanup(self):
distutils.dir_util.remove_tree(os.path.join(os.getcwd(), '.pydy_viz'))
dst = os.path.join(os.getcwd(), 'static')

if os.path.exists(dst) and overwrite is False:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am fine with the changes, but I feel we should be writing tests too for the same(because of conditional statements). I can write them and if we are short of time(sending tutorial instructions to students), then we can merge it and tests can be sent in a separate PR.

Copy link
Member

Choose a reason for hiding this comment

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

This isn't critical to have for the tutorial, because we already have at least one browser/os combination that works for everyone. But if this was in a new pydy-viz release before the april 2, then we would have it available for backup in case the socket server isn't working for some.

ans = raw_input("The 'static' directory already exists. Would "
+ "you like to overwrite the contents? [y|n]\n")
if ans == 'y':
shutil.rmtree(dst)
overwrite = True
elif os.path.exists(dst) and overwrite is True:
shutil.rmtree(dst)
elif not os.path.exists(dst):
overwrite = True

if overwrite is True:
src = os.path.join(os.path.dirname(pydy_viz.__file__), 'static')
print("Copying static data.")
shutil.copytree(src, dst)
print("Copying Simulation data.")
_outfile_loc = os.path.join(os.getcwd(), 'static', 'data.json')
outfile = open(_outfile_loc, "w")
# For static rendering, we need to define json data as a
# JavaScript variable.
outfile.write('var JSONObj=')
outfile.write(json.dumps(self._data_dict, indent=4,
separators=(',', ': ')))
outfile.write(';')
outfile.close()
print("To view the visualization, open {}".format(
os.path.join(dst, 'index.html')) +
" in a WebGL compliant browser.")
else:
print('Aborted.')

def remove_static_html(self, force=False):
"""Removes the ``static`` directory from the current working
directory.

Parameters
----------
force : boolean, optional, default=False
If true, no warning is issued before the removal of the
directory.

"""
if os.path.exists('static'):
if force is False:
ans = raw_input("Are you sure you would like to delete the " +
"'static' directory? [y|n]\n")
if ans == 'y':
force = True

if force is True:
print 'Cleaning up static directory..'
distutils.dir_util.remove_tree(os.path.join(os.getcwd(),
'static'))
print 'All Done!'
else:
print('Aborted.')

def _display_from_interpreter(self):
server = Server(json=self.saved_json_file)
Expand Down