Skip to content

Commit

Permalink
Fully working multiple consoles. and it doesn't break existing posts
Browse files Browse the repository at this point in the history
  • Loading branch information
gerbal committed Feb 7, 2014
1 parent edfa504 commit 766368c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 67 deletions.
8 changes: 4 additions & 4 deletions _includes/endpython
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```
</textarea>
<br /><button>Run</button>
<p>Output: (<a href="#" id="clearoutput">clear</a>)</p>
<pre id="edoutput">Press Run to see the output of the code above</pre>
<canvas id="mycanvas" height="400" width="400" style="border-style: solid; display: none"></canvas>
<br /><button id='btn{{include.id}}'>Run</button>
<p>Output: (<a href="#" id="clearoutput{{include.id}}">clear</a>)</p>
<pre id="edoutput{{include.id}}">Press Run to see the output of the code above</pre>
<canvas id="mycanvas{{include.id}}" height="400" width="400" style="border-style: solid; display: none"></canvas>
8 changes: 2 additions & 6 deletions _includes/python
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<script type="text/javascript">
loadCSS("{{ site.baseurl}}/css/codemirror.css");
loadCSS("{{ site.baseurl}}/css/monokai.css");
loadJS("{{ site.baseurl}}/js/codemirrorepl.js");
loadJS("{{ site.baseurl}}/js/python.js");
loadJS("{{ site.baseurl}}/js/editor.js");
idmods.push('{{include.id}}')
</script>
<textarea id='code' rows="12" cols="80">
<textarea id='code{{include.id}}' rows="12" cols="80">

```
7 changes: 6 additions & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt.min.js" type="text/javascript"></script>
<script src="http://www.skulpt.org/static/skulpt-stdlib.js" type="text/javascript"></script>

<script type="text/javascript">var idmods = new Array();</script>
<link rel="stylesheet" type="text/css" href="/spring2014/css/codemirror.css">
<script src="/spring2014/js/codemirrorepl.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="/spring2014/css/monokai.css">
<script src="/spring2014/js/python.js" type="text/javascript"></script>
<script src="/spring2014/js/editor.js" type="text/javascript"></script>
{% endif %}
<script src="{{ site.baseurl}}/js/date-cs.js" type="text/javascript"></script>

Expand Down
60 changes: 30 additions & 30 deletions _posts/2014-01-31-stringsandlists.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ I am including a few exercises I did in the strings chapter beyond the ones we w
although not the optional ones you mentioned (I did not start early enough and now I am almost out of time).
To begin with, here is #2:

```
{% include python id='1' %}

prefixes = "JKLMNOPQ"
suffixa = "ack"
Expand All @@ -19,10 +19,10 @@ for p in prefixes:
else:
print(p + suffixb)

```
{% include endpython id='1' %}
Here is its output:

```
{% include python id='K' %}

Jack
Kack
Expand All @@ -33,11 +33,11 @@ Ouack
Pack
Quack

```
{% include endpython id='K' %}

And here is #3:

```
{% include python id='A' %}

alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
# This is not actually my favorite paragraph of text;
Expand All @@ -58,19 +58,19 @@ for ch in best_text:

print("Your text contains", number_of_letters, "alphabetic characters, of which", number_of_es, "are the letter E.")

```
{% include endpython id='A' %}

Here is the output from #3:

```
{% include python id='S' %}

Your text contains 243 alphabetic characters, of which 109 (44.8%) are 'e'.

```
{% include endpython id='S' %}

6.

```
{% include python id='D' %}

from test import testEqual

Expand All @@ -94,14 +94,14 @@ def reverse_by_word(old_string):
return new_string
print(reverse_by_word("Son I am able she said / though you scare me watch said I / beloved I said watch me scare you though / said she able am I son"))

```
{% include endpython id='D' %}

7.
Am I allowed to copy the code which was already in the textbook as the answer to this problem?
It was surprisingly concise and I don't see how I could improve upon it, except to try different words
with the "testEqual" function. All the tests yielded "Pass".

```
{% include python id='F' %}

from test import testEqual

Expand All @@ -117,14 +117,14 @@ def mirror(words):
testEqual(mirror('mirror'),'mirrorrorrim')
testEqual(mirror('onthewall'),'onthewallllawehtno')

````
{% include endpython id='F' %}`

11.
This next one is again the code from the text, which finally explained to me
how to do this type of thing after a long period of confusion.
All the tests yielded "Pass", of course.

```
{% include python id='G' %}

from test import testEqual

Expand All @@ -139,14 +139,14 @@ testEqual(remove('an', 'banana'),'bana')
testEqual(remove('cyc', 'bicycle'), 'bile')
testEqual(remove('iss', 'Mississippi'), 'Missippi')

```
{% include endpython id='G' %}

12.
Here is exercise #12 for the strings chapter; it does not quite get unlimited instances
of a substring out of a string, but I did figure out how to take two instances out,
which was something, at least.

```
{% include python id='R' %}

def remove(substr, theStr):
indexa = theStr.index(substr)
Expand All @@ -163,7 +163,7 @@ print(remove('an', 'banana'))
print(remove('cyc', 'bicycle'))
print(remove('I', 'I palindrome I'))

```
{% include endpython id='R' %}
The output was "ba", "bile", and " palindrome" (I had not worked out anything to remove spaces).

Here are the exercises from the lists chapter:
Expand All @@ -172,7 +172,7 @@ Here are the exercises from the lists chapter:
7.
Here is the code provided by the text, slightly modified:

```
{% include python id='T' %}

def countOdd(lst):
odd_nums = 0
Expand All @@ -183,13 +183,13 @@ def countOdd(lst):

print(countOdd([1,3,5,6,7]))

```
{% include endpython id='T' %}

The output is of course 4.

Here is #8:

```
{% include python id='Y' %}

def sum_even(numlist):
even_sum = 0
Expand All @@ -199,12 +199,12 @@ def sum_even(numlist):
return even_sum
print sum_even([5,3,5,6,8,2])

```
{% include endpython id='Y' %}
The output is 16.

Here is #9:

```
{% include python id='U' %}

def sum_neg(thelist):
neg_sum = 0
Expand All @@ -214,7 +214,7 @@ def sum_neg(thelist):
return neg_sum
print(sum_neg([0, -3, 4, -12, 5]))

```
{% include endpython id='U' %}

The output from #9 was -15.

Expand All @@ -227,7 +227,7 @@ rather than words.
In this version I am making it replace particular characters, which was
also exercise #8 in the strings chapter.

```
{% include python id='I' %}

def replace(s, old, new):
clever_string = ""
Expand All @@ -248,27 +248,27 @@ print(s)
s = replace(s, "I", "A")
print(s)

```
{% include endpython id='I' %}

The output is like so:

```
{% include python id='O' %}

I am a spaghetti noodle.
I am a spaghetti naadle.
I am a spaghatti naadla.
I am a spaghatta naadla.
A am a spaghatta naadla.

```
{% include endpython id='O' %}

Here I am attempting to replace strings, but right now it does not allow more than
two instances of the substring. It also does not use "split" or "join;"
my experiments were those have not been successful so far.
I am running out of time though, so here is what I have now.


```
{% include python id='P' %}

def replace(s, old, new):

Expand All @@ -285,11 +285,11 @@ def replace(s, old, new):
print(replace("pineapple", "pine", "PINE"))
print(replace("pine-pine-apple", "pine", "PINE"))

```
{% include endpython id='P' %}

The output is like so:

```
{% include python id='4' %}

0
0
Expand All @@ -298,4 +298,4 @@ PINEapple
5
PINE-PINE-apple

```
{% include endpython id='4' %}
20 changes: 19 additions & 1 deletion _posts/how-tos/2014-01-21-consoles.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,28 @@ You'll need some include statements to do this. Include statements pull in code
```
{% raw %}{% include python %}
for i in ["I", "love", "Python"}:
for i in ["I", "love", "Python"]:
print i
{% include endpython %}{% endraw %}
```

If you want more than one console in your post, add `id='uniqueID'` to the includes statement. Each console needs a unique id value:

```
{% raw %}{% include python id='1'%}
for i in ["I", "love", "Python", "too"]:
print i
{% include endpython id='1' %}{% endraw %}
```

{% include python id='1'%}

for i in ["I", "love", "Python", "too"]:
print i

{% include endpython id='1' %}

A HUGE thanks to SILShacker and TA Grant McLendon for getting this working and looking pretty.
4 changes: 2 additions & 2 deletions console.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: default
title: Python Console
---

{% include python %}
{% include python id='1' %}
print "Type and run your python code here"
print "="*25
import turtle
Expand All @@ -12,4 +12,4 @@ for c in ['red', 'green', 'yellow', 'blue']:
t.color(c)
t.forward(75)
t.left(90)
{% include endpython %}
{% include endpython id='1' %}
Loading

0 comments on commit 766368c

Please sign in to comment.