Skip to content

Commit 4b4b3d1

Browse files
committed
Add download function
1 parent 767fa8f commit 4b4b3d1

File tree

9 files changed

+26
-11
lines changed

9 files changed

+26
-11
lines changed

GlowScriptOffline/glowscript_libraries/RScompiler.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GlowScriptOffline/glowscript_libraries/compiler.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GlowScriptOffline/glowscript_libraries/glow.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/VPythonDocs/files.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
</table>
4444
<div></div>
4545
<h1 class="Heading-1"><font color="#0000a0">File operations</font></h1>
46-
<p class="Normal">In Web VPython, you can import an external JavaScript library or read a file on the user's local computer. <em>The following information does not apply to VPython 7, where one can use the standard Python methods for importing modules and reading or writing files.</em></p>
46+
<p class="Normal">In Web VPython, you can import an external JavaScript library, read a file on the user's local computer, or download a file containing text generated by your program. <em>The following information does not apply to VPython 7, where one can use the standard Python methods for importing modules and reading or writing files.</em></p>
4747
<p class="Normal"></p>
4848
<h1 class="Heading-1"><font color="#0000a0">get_library: import a library</font></h1>
4949
<p class="Normal">To import a JavaScript library, do this:</p>
@@ -71,9 +71,13 @@ <h1 class="Heading-1"><font color="#0000a0">read_local_file: user chooses a file
7171
<p class="Normal">If you say <strong>read_local_file()</strong>, the button is appended to the bottom of the window, corresponding to the jQuery location $('body').</p>
7272
<div></div>
7373
<div class="Normal"></div>
74-
<h1 class="Heading-1"><font color="#0000a0">Writing a file</font></h1>
75-
<p class="Normal">Due to security issues, it is not possible to write directly to the user's computer storage from a browser. Currently the only way to write data to a local file is to print the data, select the print area (ctrl-A), copy (ctrl-C), paste into a local text editor, and save the file.</p>
76-
<p class="Normal">&nbsp;</p>
74+
<h1 class="Heading-1"><font color="#0000a0">d</font><font color="#0000a0">ownload: writing a file</font></h1>
75+
<p class="Normal">Due to security issues, it is not possible to write directly from a browser to an arbitrary file in the user's computer storage. However, a browser can write to the user's download folder. In Web VPython you can write a string to the download folder using the download function:</p>
76+
<p class="program">download(filename, data) <br />
77+
</p>
78+
<p class="Normal">Both the filename and the data must be strings. A useful way to create the data string is to use Python f-strings; see <a href="text_output.html"><strong>Text Output</strong></a>. Note that repeated execution with the same file name results in the later downloaded files having (1), (2), (3)..added .to the file name.</p>
79+
<p class="Normal">&nbsp; </p>
80+
<p class="Normal">&nbsp;</p>
7781
<!-- InstanceEndEditable -->
7882
</div>
7983
</div>

lib/compiling/GScompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ where compile() was called, in untrusted/run.js.
772772
"vec", "vector", "rate", "sleep", "update", "color", "paths", "shapes", "canvas",
773773
"vertex", "triangle", "quad", "label", "distant_light", "local_light", "attach_trail", "attach_arrow",
774774
"sqrt", "pi", "sin", "cos", "tan", "asin", "acos", "atan", "atan2", "exp", "log", "pow",
775-
"factorial", "combin", "button", "radio", "checkbox", "slider", "checkbox", "text",
775+
"factorial", "combin", "button", "radio", "checkbox", "slider", "checkbox", "text", "download",
776776
"radians", "degrees", "get_library", "read_local_file"]
777777

778778
if (options.lang == 'vpython') {

lib/glow/api_misc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@
6262
return info
6363
}
6464

65+
function download(filename, data){ // send data file to Download directory
66+
if (filename.constructor !== String) throw new Error('A download file name must be a string.')
67+
if (data.constructor !== String) throw new Error('The download data must be a string.')
68+
let a = document.createElement("a")
69+
a.setAttribute('href', 'data:text/text;charset=utf-8,' + encodeURI(data));
70+
a.setAttribute('download', filename);
71+
a.click()
72+
a.remove()
73+
}
74+
6575
// http://my.opera.com/edvakf/blog/how-to-overcome-a-minimum-time-interval-in-javascript
6676
// This function is supposed to return almost instantly if there is nothing pending to do
6777
/*
@@ -789,6 +799,7 @@
789799
msclock: msclock,
790800
get_library: get_library,
791801
read_local_file: read_local_file,
802+
download: download,
792803
parse_html,
793804
display_2D,
794805
pytype

package/RScompiler.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/compiler.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/glow.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)