You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<pclass="Normal">In GlowScript 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>
<pclass="Normal"></p>
<h1class="Heading-1"><fontcolor="#0000a0">get_library: import a library</font></h1>
<pclass="Normal">To import a JavaScript library, do this:</p>
<pclass="Normal"> If the file cannot be found within a reasonable amount of time, an error message is displayed.</p>
<pclass="Normal"> Currently the library must be written in JavaScript, not RapydScript, or VPython. Moreover, there are restrictions imposed because the library doesn't go through the special preprocessing that occurs when you run your program at glowscript.org. The library cannot include options that require using <strong>"rate"</strong> or "<strong>waitfor</strong>", and the following vector operations must be rewritten as shown, where <strong>A</strong> and <strong>B</strong> are vectors and <strong>k</strong> is an ordinary number:</p>
<pclass="program">A+B -> A.add(B)<br/>
A-B -> A.sub(B)<br/>
k*A -> A.multiply(k)<br/>
A/k -> A.divide(k) <br/>
</p>
<pclass="Normal">However, when editing a file at glowscript.org in any language, clicking "Share this program" outputs code that has been converted to executable JavaScript, so you may find it useful to start from, say, RapydScript or VPython. You may have to make adjustments to the generated JavaScript.</p>
<pclass="Normal">It is not currently possible to import from your own glowscript.org files, so the library must be placed on your own web site.</p>
<div></div>
<divclass="Normal"></div>
<h1class="Heading-1"><fontcolor="#0000a0">read_local_file: user chooses a file</font></h1>
<pclass="Normal">The reading and writing of files is very different in a web browser, due to security issues (you don't want an arbitrary web page to read and write your computer files). You can ask the user of your program to choose a text file on the user's computer and then use the contents of the file. The function <strong>read_local_file</strong> appends a button that says "Choose File" on the screen at the location specified, such as <strong>scene.title_anchor</strong> or <strong>scene.caption_anchor</strong>. When the user clicks that button, a file dialog appears which lets the user choose a file. Then the button is removed, and information about the file is returned.</p>
<pclass="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>
<div></div>
<divclass="Normal"></div>
<h1class="Heading-1"><fontcolor="#0000a0">Writing a file</font></h1>
<pclass="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>