Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>

<head>
  <title> PyScript, MatPlotLib and Chartjs Test</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
  <script defer src="https://pyscript.net/latest/pyscript.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>

<body onload="graph()">

  <py-config>
    packages = ["matplotlib", "numpy"]
  </py-config>
  
  <div style="height:30px;"></div>
  
  <h1>PyScript / MatPlotLib and Chart.js Data Visualization</h1>
  <p class="par">Example experimental data. Press button to display.
  </p>
  
  <!-- Button to load graph with graph.js -->
  <button onclick="graph();" style="margin-top:30px;">Run Script</button>
  
  <div class="row">
    <div class="column leftcol">
      <p>PyScript plot using MatPlotLib:</p>

      <div style="background-color:white; width: 400px; margin-left:auto; margin-right:auto;">
        <div id="mpl" style="height:400px; opacity: 0;"></div>
      </div>

      <div style="height:30px;"></div>
    </div>
    <div class="column rightcol">
      <p>Chart.js plot (from python data):</p>

      <div style="padding: 0px; margin:auto; width:370px; height:370px; background-color:white; padding: 15px"><canvas id="myChart"></canvas></div>
      <div style="height:30px;"></div>
    </div>
  </div>

  <!-- Python Code -->
  <py-script>
    import numpy as np
    import matplotlib.pyplot as plt

    x = [0,0.10,0.20,0.30,0.40,0.50,0.60,0.70,0.80,0.90,1.00,1.10,1.20,1.30,1.40,1.50,1.60,1.70,1.80,1.90,2.00,2.10,2.20,2.30,2.40,2.50,2.60,2.70,2.80,2.90,3.00,3.10,3.20,3.30,3.40,3.50,3.60,3.70,3.80,3.90,4.00]
    y =[0,0,0,0.04280958124685598,0.05888769026880673,0.09380263980676382,0.10950604591276904,0.13825155415959542,0.1535797717632878,0.17735462488942738,0.19220937950587405,0.21196412843159734,0.22611519039265204,0.24250028240950397,0.25599097021366896,0.2701301511712553,0.28253435381448366,0.29477978954203776,0.3059064064668828,0.3165610363653617,0.32640918584356954,0.3357144036979789,0.34438480875587,0.35260267315675353,0.35973726676893564,0.3663271335913618,0.37218039621844007,0.37751857321596216,0.3822937452320414,0.3866034794345888,0.3904446026377889,0.3938628223724453,0.39684166766370554,0.40009493458220413,0.4032117703626075,0.4064049297336598,0.40954951298651393,0.4127010728189676,0.4158208058411702,0.418919672315465,0.4220077032661037]

    def plot(x, y):
       plt.rcParams["figure.figsize"] = (4,4)
       fig = plt.figure()
       plt.title("PyScript / MatPlotLib Plot")
       plt.plot(x, y, marker ='.')
       return fig

    f = plot(x, y)

    display(f, target="mpl", append=False)
  </py-script>

</body>

</html>
              
            
!

CSS

              
                body {
  background: #222;
    color: white;
    text-align: center;
    font-family: 'Raleway', sans-serif;
}

h1 {
  margin-top:-30px;
}

.par {
  margin-top:-20px;
  margin-bottom:-10px;
}

/* Global button styles */
button {
  padding-left: 15px; 
  padding-right: 15px;
  padding-top: 10px;
  padding-bottom: 10px;
  font-size: large;
  color: white;
  background-color: #222;
  border: none;
  outline: 4px solid white;
  border-radius: 10px;
  transition: all .1s ease-in-out;
}

button:hover {
  cursor: pointer;
  background-color: #333;
  transform: scale(0.95);
}

/* Grid layout styles*/
.column {
  float: left;
  text-align: center;
  width: 550px;
}

.leftcol {margin-left:16.5vw;}

.rightcol{margin-left:-120px;}

@media only screen and (max-width: 1100px) {
  .leftcol {margin-left:0px;}

  .rightcol{margin-left:-130px;}
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}
              
            
!

JS

              
                // This Pen shows multiple implementations:

// - PyScript:
//     Allows running of python code within
//     the <py-script> html tag

// - Python MatPlotLib:
//     Allows for plotting of data which
//     is returned via the PyScript function
//     called display()

// - Chart.js:
//     Offered as an alternative graphing
//     solution. Note that the data values are
//     obtained directly from the python code via
//     passing in between python and javascript
//     using: pyscript.interpreter.globals.get()

// Note: Currently javascript is executed via
// button, as it loads before PyScript runs.
// In the future this could be handled by
// running the javascript function at the
// end of the initial pyscript run.

const ctx = document.getElementById("myChart");
var names = [];
var data = [];

function graph() {
  
  data = Array.from(pyscript.interpreter.globals.get("y"));
  names = Array.from(pyscript.interpreter.globals.get("x"));
  console.log(names);
  console.log(data);
  var myChart = new Chart(ctx, {
    type: "line",
    data: {
      labels: names,
      datasets: [
        {
          label: "Data",
          data: data,
          pointRadius: 3
        }
      ]
    },
    options: {
      maintainAspectRatio: false,
      responsive: true,
      plugins: {
        legend: {
          display: false
        },
        title: {
          display: true,
          text: "Chart.js Line Chart"
        }
      }
    }
  });
  
  document.getElementById("mpl").style.opacity = "1";
}

              
            
!
999px

Console