Skip to main content
Chemistry LibreTexts

5.2.6: How to Incorporate Data Files in Your Code Blocks- For SageMath

  • Page ID
    216020
  • There are a number of ways data files and can be incorporated into code blocks. However, ensure that you have already followed the steps in the introduction to the CKEditor Binder Plugin Tutorial here.

    Method 1: Using Libraries

    The CKEditor Binder Plugin supports Python which is what SageMath runs on, libraries that are supported by our version of Python may be used to read in the file. An example code block is provided below. To reuse this, simply exchange the provided url with the one attained from your own page.

    import csv
    import urllib2
    import ssl
    import codecs
    
    url = "https://chem.libretexts.org/@api/deki/files/265970/auto-mpg.data"
    request = urllib2.Request(url)
    response = urllib2.urlopen(request, context=ssl._create_unverified_context())
    csvfile = csv.reader(codecs.iterdecode(response, 'utf-8'))
    for line in csvfile:
      print(line)
    

    Here's a quick example of the things you can do using SageMath. Run the cell and your eyes will be blessed by a wonderful fully 3D parametric egg.

    f = (lambda u,v: cos(u), lambda u,v: sin(u)+cos(v), lambda u,v: sin(v))
    parametric_plot3d(f, (0,2*pi), (-pi,pi))
     

     

    • Was this article helpful?