From abcd44d864a81bc70e9fb7ee5f3f0920024013f9 Mon Sep 17 00:00:00 2001 From: Tarun Gaba Date: Mon, 31 Mar 2014 00:18:05 +0530 Subject: [PATCH 1/2] Added create_static method to Scene class. It allows you to create static webpages for the visualization. Added cleanup_static method to remove the static data generated! --- pydy_viz/scene.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/pydy_viz/scene.py b/pydy_viz/scene.py index f2e9665..ab7f180 100644 --- a/pydy_viz/scene.py +++ b/pydy_viz/scene.py @@ -316,24 +316,45 @@ def generate_visualization_json(self, dynamic_variables, separators=(',', ': '))) outfile.close() - def _copy_static_dir(self): + def create_static(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 + a directory named static in the current working directory. - Working directory can be cleaned by calling _cleanup() - method, which deletes the .pydy_viz directory. + + This method is used to output static 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. """ - dst = os.path.join(os.getcwd(), '.pydy_viz') - os.mkdir(dst) + dst = os.path.join(os.getcwd(), 'static') + try: + os.mkdir(dst) + except OSError: + print 'Directory already exists, refreshing contents..' + src = os.path.join(os.path.dirname(pydy_viz.__file__), 'static') + print "Copying static data.." distutils.dir_util.copy_tree(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 "All Done!" - def _cleanup(self): - distutils.dir_util.remove_tree(os.path.join(os.getcwd(), '.pydy_viz')) + def cleanup_static(self): + print 'Cleaning up static directory..' + distutils.dir_util.remove_tree(os.path.join(os.getcwd(), 'static')) + print 'All Done!' def _display_from_interpreter(self): server = Server(json=self.saved_json_file) From b1c89c6e2b52f2713048c4c63c7d229392206d25 Mon Sep 17 00:00:00 2001 From: "Jason K. Moore" Date: Sun, 30 Mar 2014 22:03:48 -0400 Subject: [PATCH 2/2] Prevented unwanted file deletion and improve method names. Safety code is in place in create_static_html and remove_static_html to prevent unwanted deletions. I also changed the names of the methods to indicate more precisely what the methods do. --- pydy_viz/scene.py | 110 +++++++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 35 deletions(-) diff --git a/pydy_viz/scene.py b/pydy_viz/scene.py index ab7f180..942aff8 100644 --- a/pydy_viz/scene.py +++ b/pydy_viz/scene.py @@ -1,4 +1,5 @@ import os +import shutil import json import distutils import distutils.dir_util @@ -316,45 +317,84 @@ def generate_visualization_json(self, dynamic_variables, separators=(',', ': '))) outfile.close() - def create_static(self): - """ - Copies all the static files required in the - visualization to the current working directory - The files and sub directories are stored within - a directory named static in the current - working directory. - - This method is used to output static 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. + 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(), 'static') - try: - os.mkdir(dst) - except OSError: - print 'Directory already exists, refreshing contents..' - - src = os.path.join(os.path.dirname(pydy_viz.__file__), 'static') - print "Copying static data.." - distutils.dir_util.copy_tree(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 "All Done!" - def cleanup_static(self): - print 'Cleaning up static directory..' - distutils.dir_util.remove_tree(os.path.join(os.getcwd(), 'static')) - print 'All Done!' + if os.path.exists(dst) and overwrite is False: + 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)