Skip to content
Snippets Groups Projects
Commit 3f199919 authored by Anton Akhmerov's avatar Anton Akhmerov
Browse files

Merge branch 'thebelab' into 'master'

Add live code editing using thebelab

See merge request solidstate/lectures!47
parents e6c97012 562dc230
Branches
No related tags found
1 merge request!47Add live code editing using thebelab
Pipeline #17505 passed
Showing
with 239 additions and 34 deletions
...@@ -19,6 +19,7 @@ shutil.rmtree(target, ignore_errors=True) ...@@ -19,6 +19,7 @@ shutil.rmtree(target, ignore_errors=True)
target.mkdir(exist_ok=True) target.mkdir(exist_ok=True)
shutil.copytree(src / 'figures', target / 'figures') shutil.copytree(src / 'figures', target / 'figures')
shutil.copytree(src / 'scripts', target / 'scripts') shutil.copytree(src / 'scripts', target / 'scripts')
shutil.copytree(src / 'styles', target / 'styles')
shutil.copytree(src / 'x3d', target / 'x3d') shutil.copytree(src / 'x3d', target / 'x3d')
output_extractor = ExtractOutputPreprocessor() output_extractor = ExtractOutputPreprocessor()
...@@ -34,7 +35,7 @@ exporter = nbconvert.MarkdownExporter( ...@@ -34,7 +35,7 @@ exporter = nbconvert.MarkdownExporter(
nbconvert.preprocessors.ExecutePreprocessor, nbconvert.preprocessors.ExecutePreprocessor,
output_extractor, output_extractor,
], ],
exclude_input=True, exclude_input=False,
template_file='extra_markdown.tpl', template_file='extra_markdown.tpl',
), ),
NbConvertBase=dict( NbConvertBase=dict(
......
{% extends 'markdown.tpl' %} {% extends 'markdown.tpl' %}
{%- block data_other -%} {% block input %}
{%- for type in output.data | filter_data_type -%} {% if cell.metadata.attributes and cell.metadata.attributes.initialize %}
{%- if type == 'application/vnd.plotly.v1+json' -%} <div class="thebelab-init-code">
{%- set fname = output.metadata.filenames['application/vnd.plotly.v1+json'] -%} <pre class="thebelab-code" data-executable="true" data-language="python">
{%- set plotly_url = fname | path2url -%} {{cell.source}}
{%- set div_id = fname.split('/') | last -%} </pre>
<div id="{{div_id}}"><img ></img></div> </div>
{% endif %}
{% endblock %}
<script> {% block display_data %}
window.addEventListener('load', function() { {% if cell.metadata.attributes and cell.metadata.attributes.editable == 'false' and not cell.metadata.attributes.initialize %}
Plotly.d3.json('../{{plotly_url}}', function(error, fig) { {{ super() }}
Plotly.plot('{{div_id}}', fig.data, fig.layout).then( {% elif cell.metadata.attributes and cell.metadata.attributes.initialize %}
function(value) {Plotly.relayout('{{div_id}}', {height: ' '})} {% if output.data['text/html'] %}
); <div>
}); {{ output.data['text/html'] }}
}); </div>
</script> {% endif %}
{%- endif -%} {% else %}
{%- endfor -%} <pre class="thebelab-code" data-executable="true" data-language="python">{{cell.source}}</pre>
{%- endblock -%} <div class="thebelab-output" data-output="true" markdown="1">
{{ super() }}
</div>
{% endif %}
{% endblock %}
{% block error %} {% block error %}
{% endblock error %} {% endblock error %}
......
...@@ -46,13 +46,19 @@ markdown_extensions: ...@@ -46,13 +46,19 @@ markdown_extensions:
permalink: True permalink: True
- admonition - admonition
- pymdownx.details - pymdownx.details
- pymdownx.extra
- abbr - abbr
- footnotes - footnotes
- meta - meta
extra_css:
- 'https://use.fontawesome.com/releases/v5.8.1/css/all.css'
- 'styles/thebelab.css'
extra_javascript: extra_javascript:
- 'https://cdn.plot.ly/plotly-latest.min.js'
- 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-AMS_HTML' - 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-AMS_HTML'
- 'scripts/thebelab-custom.js'
- 'scripts/mathjaxhelper.js' - 'scripts/mathjaxhelper.js'
- 'scripts/thebelabhelper.js'
- 'scripts/x3dom.js' - 'scripts/x3dom.js'
copyright: "Copyright © 2017-2019 Delft University of Technology, CC-BY-SA 4.0." copyright: "Copyright © 2017-2019 Delft University of Technology, CC-BY-SA 4.0."
```python ```{python initialize=true}
import numpy as np import numpy as np
import plotly.offline as py import plotly.offline as py
import plotly.graph_objs as go import plotly.graph_objs as go
......
```python ```{python initialize=true}
import numpy as np import numpy as np
import plotly.offline as py import plotly.offline as py
import plotly.graph_objs as go import plotly.graph_objs as go
pi = np.pi pi = np.pi
py.init_notebook_mode(connected=True)
``` ```
# Tight binding and nearly free electrons # Tight binding and nearly free electrons
......
```python ```{python initialize=true}
from matplotlib import pyplot from matplotlib import pyplot
......
...@@ -12,7 +12,7 @@ jupyter: ...@@ -12,7 +12,7 @@ jupyter:
name: python3 name: python3
--- ---
```python ```{python initialize=true}
import pandas import pandas
from matplotlib import pyplot from matplotlib import pyplot
...@@ -26,6 +26,8 @@ import plotly.graph_objs as go ...@@ -26,6 +26,8 @@ import plotly.graph_objs as go
from common import draw_classic_axes, configure_plotting from common import draw_classic_axes, configure_plotting
configure_plotting() configure_plotting()
py.init_notebook_mode(connected=True)
``` ```
_(based on chapter 2.1 of the book)_ _(based on chapter 2.1 of the book)_
...@@ -44,6 +46,7 @@ _(based on chapter 2.1 of the book)_ ...@@ -44,6 +46,7 @@ _(based on chapter 2.1 of the book)_
Let us look at the heat capacities of different chemical elements[^1]: Let us look at the heat capacities of different chemical elements[^1]:
```python ```python
elements = pandas.read_json('elements.json') elements = pandas.read_json('elements.json')
elements.full_name = elements.full_name.str.capitalize() elements.full_name = elements.full_name.str.capitalize()
hovertext = elements.T.apply( hovertext = elements.T.apply(
......
...@@ -12,7 +12,7 @@ jupyter: ...@@ -12,7 +12,7 @@ jupyter:
name: python3 name: python3
--- ---
```python ```{python initialize=true}
from matplotlib import pyplot from matplotlib import pyplot
import numpy as np import numpy as np
......
```python ```{python initialize=true}
from matplotlib import pyplot from matplotlib import pyplot
import numpy as np import numpy as np
......
```python ```{python initialize=true}
from matplotlib import pyplot from matplotlib import pyplot
import numpy as np import numpy as np
......
```python ```{python initialize=true}
from matplotlib import pyplot from matplotlib import pyplot
import numpy as np import numpy as np
......
```python ```{python initialize=true}
from matplotlib import pyplot from matplotlib import pyplot
import numpy as np import numpy as np
......
```python ```{python initialize=true}
import matplotlib import matplotlib
from matplotlib import pyplot from matplotlib import pyplot
......
```python ```{python initialize=true}
import matplotlib import matplotlib
from matplotlib import pyplot from matplotlib import pyplot
......
...@@ -12,7 +12,7 @@ jupyter: ...@@ -12,7 +12,7 @@ jupyter:
name: python3 name: python3
--- ---
```python ```{python initialize=true}
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
...@@ -24,6 +24,8 @@ import plotly.offline as py ...@@ -24,6 +24,8 @@ import plotly.offline as py
import plotly.graph_objs as go import plotly.graph_objs as go
configure_plotting() configure_plotting()
py.init_notebook_mode(connected=True)
``` ```
# Lecture 9 – Crystal structure # Lecture 9 – Crystal structure
......
```python ```{python initialize=true}
from matplotlib import pyplot from matplotlib import pyplot
import numpy as np import numpy as np
......
This diff is collapsed.
function initThebelab() {
let activateButton = document.getElementById("thebelab-activate-button");
if (activateButton.classList.contains('thebelab-active')) {
return;
}
thebelab.bootstrap();
// Run all thebelab cells marked as initialization code
setTimeout(function() {
let initDivs = document.getElementsByClassName("thebelab-init-code");
for(var i = 0; i < initDivs.length; i++) {
// div.thebelab-init-code > div.thebelab-cell > button.thebelab-run-button
let runButton = initDivs[i].firstElementChild.childNodes[1]
runButton.click();
}
}, 500)
activateButton.classList.add('thebelab-active')
}
\ No newline at end of file
.thebelab-cell .thebelab-input pre {
z-index: 0;
}
.thebelab-code, .thebelab-init-code {
display: none;
}
.thebelab-button {
position: relative;
display: inline-block;
box-sizing: border-box;
border: none;
border-radius: .1rem;
padding: 0 2rem;
margin: .5rem .1rem;
min-width: 64px;
height: 1.6rem;
vertical-align: middle;
text-align: center;
font-size: 0.8rem;
color: rgba(0, 0, 0, 0.8);
background-color: rgba(0, 0, 0, 0.07);
overflow: hidden;
outline: none;
cursor: pointer;
transition: background-color 0.2s;
}
.thebelab-button:hover {
background-color: rgba(0, 0, 0, 0.12);
}
.thebelab-button:active {
background-color: rgba(0, 0, 0, 0.15);
color: rgba(0, 0, 0, 1)
}
#thebelab-activate-button {
font-size: 1rem;
margin-left: 0.4rem;
}
#thebelab-activate-button.thebelab-active {
color: rgb(83, 109, 254);
}
.thebelab-button.thebelab-restart-button {
display: none;
}
...@@ -17,3 +17,42 @@ ...@@ -17,3 +17,42 @@
</script> </script>
<!-- End Piwik Code --> <!-- End Piwik Code -->
{% endblock %} {% endblock %}
{% block scripts %}
<!-- ThebeLab config -->
<!-- IMPORTANT: Everytime a python file is changed, ref needs to be updated to the latest commit! -->
<script type="text/x-thebe-config">
{
predefinedOutput: true,
binderOptions: {
repoProvider: "git",
repo: "https://gitlab.kwant-project.org/solidstate/lectures",
ref: "e6c970126f4e819e4a3eb717a86ba9fb523d20bf",
},
requestKernel: true,
kernelOptions: {
name: "python3",
path: "src"
},
}
</script>
<!-- End ThebeLab config -->
{{ super() }}
{% endblock %}
{% block libs %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" type="text/javascript"></script>
{{ super() }}
{% endblock %}
{% block content %}
{% if 'thebelab-code' in super() %}
<a title="Make live" id="thebelab-activate-button" class="md-icon md-content__icon" onclick="initThebelab()">
<i class="fas fa-magic"></i>
</a>
{{ super() }}
{% else %}
{{ super() }}
{% endif %}
{% endblock %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment