Skip to main content
Chemistry LibreTexts

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

  • Page ID
    216020
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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))
     

     


    5.2.6: How to Incorporate Data Files in Your Code Blocks- For SageMath is shared under a CC BY 1.3 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?