Reading from csv file

104 views
Skip to first unread message

Pat Counts

unread,
Jun 29, 2024, 5:54:58 AM6/29/24
to VPython-users
Newbie here. I’m writing a program to simulate a model rocket launch. I have a csv file with thrust force vs time data and am wondering if glowscript (WebVPython?) running in a browser can read data from that csv file. Is this possible? I usually code and run in Trinket, but I can use some other tool if that is better.

Bruce Sherwood

unread,
Jun 29, 2024, 6:34:54 PM6/29/24
to VPython-users
See https://www.glowscript.org/docs/VPythonDocs/file.html

Here is a Web VPython program that reads several JavaScript files that contain data:

Pat Counts

unread,
Jun 30, 2024, 11:45:16 AM6/30/24
to VPython-users
Thanks Bruce. I did read that documentation. I wish I knew how to convert CSV data into javascript. I guess I'm in over my head. I don't know the first thing about javascript. I've gotten pretty good enough at writing basic VPython code that works by itself for the physics I teach, but I've never had it interact with anything on the outside. Does get_library grab data from the javascript program hosted somewhere else? Basically I have a CSV file of thrust force versus time, and I want to use it in a loop that repeatedly calculates the acceleration, velocity, and position values as time increases by dt = 0.001. I can do everything but get the thrust data from the CSV. I would enter the data as a list, but it is thousands of data points long (a thousand data points collected per second for at least three seconds). Can you recommend a resource for learning just enough in the short term to let me do what I want to happen? If it is too big of an ask, no problem. I might be trying to run before I can walk. But in any case, let me just thank you for all you've done in providing such a great tool for us.

Bruce Sherwood

unread,
Jun 30, 2024, 3:42:48 PM6/30/24
to VPython-users
Look at the beginning of tinyurl.com/SurfaceCharge to find the urls of the JavaScript files that the Web VPython program references. For example, here is one of those files:


Just put this url in a browser and inspect the file, which contains very simple JavaScript that is just functions that return data to the Web VPython program. No big deal. You don't need to be expert with JavaScript. You will however either need to host your file somewhere.

Another possibility is to give your users a copy of your CSV file to put on their own computer, and access the data using read_local_file. See 


Bruce

John

unread,
Jun 30, 2024, 8:11:45 PM6/30/24
to VPython-users
Have a look at the source code of simple program for reading a CSV file into webvpython


It can read the attached CSV file when you run the program and click on "Choose File" and choose the file "samplecsv.csv" on your computer. If you have your own large csv file it might take a while to load the file.

For regular python where you can import python packages (not webvpython) you can look at this tutorial for reading csv files which uses the csv python package. It can probably read large csv files a lot faster than the above example.


John
samplecsv.csv

John

unread,
Jun 30, 2024, 8:22:46 PM6/30/24
to VPython-users
You asked "Does get_library grab data from the javascript program hosted somewhere else?"

If you are using regular python then have a look at the recommended answer to this question on stackoverflow


You can combine this with the information in this python csv tutorial to load your csv file from a remote host.


John

John

unread,
Jul 1, 2024, 10:10:35 AM7/1/24
to VPython-users
For regular python there is also the requests python package for retrieving files over the internet as explained in this video

John

unread,
Jul 1, 2024, 12:15:13 PM7/1/24
to VPython-users
For webvpython using read_local_file() to read large csv file from your computer I created this demo.


It prints out every 500'th line of the csv file, and it runs much faster since it doesn't print every single line. You can try it out with your file if you have a large csv file or you can try it with the attached csv file. Just click on "Choose File" button to choose the csv file on your computer.

STLbot.csv

Pat Counts

unread,
Jul 1, 2024, 4:39:14 PM7/1/24
to VPython-users
Thank you Bruce and John. This gives me a lot to think about. I’m going out of town, so I’ll have to come back to this later in July. Thanks 10**6 for your help!
Pat

Message has been deleted

Pat Counts

unread,
Sep 8, 2024, 6:26:10 PM9/8/24
to VPython-users
Hello, I'm finally back at it. I've decided to go the "choose file" button route.

I've written a program that models the motion of a model rocket using a C5-3 rocket motor that I have collected thrust data for at 1/1000th of a second increments. That data is in the attached csv file.

Eventually I will write a program that models the motion with air resistance taken into account, but to keep things simple while I figure out how to include code that grabs csv data, I have written a very simplistic VPython program that I can play with. That program is here:

(John, I looked your example, and it looks good. Thank you very much. But I can't figure out which lines are the ones I need and where to insert them. Yes, I'm that much of a beginner.)

Can you help me to add code that will allow the user to choose the csv file and use the data in the program?

Pat
Estes_C5-3_Motor_Thrust_Data_only_for_vpython.csv
Reply all
Reply to author
Forward
0 new messages