Read in data from a file.

58 views
Skip to first unread message

Michael Crowe

unread,
Mar 6, 2016, 2:29:04 PM3/6/16
to Glowscript Users
I cannot get my head around how to use read_local_file().
The paragraph title 'read_local_file:user chooses a file' is not very clear.

I need to see a program example. Is there an example program somewhere?
All Glowscript programs I have read so far do not read in external data.
Is it possible to get data in from the real world?

Tried :

GlowScript 2.1 VPython
scene.title='Help'
box()
f = read_local_file(scene.title, wait)

But just got an error.

TypeError: place.append is not a function

Line 4: f = read_local_file(scene.title, wait)

Which is not clear what that means?


Bruce Sherwood

unread,
Mar 6, 2016, 2:40:19 PM3/6/16
to Glowscript Users
The following example works, but evidently read_local_file is broken in GlowScript 2.1, of which I was not aware. Thanks for the report.

GlowScript 2.0 VPython
box()
f = read_local_file(scene.title) # "wait" is supplied when using VPython (but not when using JavaScript or RapydScript)
print(f.name) # The file name
print(f.size) # File size in bytes
print(f.type) # What kind of file
print(f.date) # Creation date if available
print('*************')
print(f.text) # The file contents

Bruce Sherwood

unread,
Mar 6, 2016, 2:46:53 PM3/6/16
to Glowscript Users
I should mention that another way to read data is to put the data in a JavaScript file somewhere on your own web site, and then use the GlowScript get_library('your url') function to import the information. Below is the start of such a file that I use with get_library(). In my GlowScript program I can get data as polarized_block.get_W() or polarized_block.get_qslength() or polarized_block.get_qs().

var polarized_block = {}

polarized_block.get_W = function () {return 0.000250}

polarized_block.get_qslength = function () {return 2560}

polarized_block.get_qs = function () { return [
    1.081306e-19, 7.382850e-20, 6.279288e-20, 5.602415e-20, 5.099137e-20, 
    4.687959e-20, 4.332160e-20, 4.011904e-20, 3.715124e-20, 3.433801e-20, 
    3.162210e-20, 2.895996e-20, 2.631647e-20, 2.366163e-20, 2.096842e-20, 
    1.821114e-20, 1.536414e-20, 1.240062e-20, 9.291370e-21, 6.003393e-21, 
    ......
    ] }

Bruce Sherwood

unread,
Mar 6, 2016, 6:40:43 PM3/6/16
to Glowscript Users
VPython 2.1 has been updated and includes corrected and improved documentation for read_local_file, which does work with VPython 2.1. Here is what the Help now says:

The reading and writing of files is very different in a web browser. 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 read_local_file appends a button that says "Choose File" on the screen at the location specified, such as scene.title_anchor or scene.caption_anchor. 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.

f = read_local_file(scene.title_anchor)


print(f.name) # The file name
print(f.size) # File size in bytes
print(f.type) # What kind of file
print(f.date) # Creation date if available

print('-------------------')


print(f.text) # The file contents

If you say read_local_file(), the button is appended to the bottom of the window, corresponding to the jQuery location $('body').

Michael Crowe

unread,
Mar 7, 2016, 1:44:33 PM3/7/16
to Glowscript Users
Thank you for the reply. Ironically, as sometimes happens, I solved the problem myself using the code given below, which works for me about 30minutes after I posted the problem. But I could not retract the post.

GlowScript 2.1 VPython
scene.title='Help'
box()
a=read_local_file()
print(a.text)

Reply all
Reply to author
Forward
0 new messages