Skip to content

Commit

Permalink
Extend Brython example to show integration with JS. Add smartdown.inc…
Browse files Browse the repository at this point in the history
…lude examples to Extensions.md. Change smartdown.use to smartdown.import
  • Loading branch information
DoctorBud committed Jul 29, 2018
1 parent 98f3601 commit b828210
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 19 deletions.
24 changes: 23 additions & 1 deletion Brython.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ I recently explored the possibility of using [Brython](http://www.brython.info)

This simple example effectively uses the browser's `window.alert()` function, but does so via Brython and the [browser](http://www.brython.info/static_doc/en/browser.html) module.

```brython/playable
```brython/playable/debug
"""A very simple Python3 program"""
import browser
Expand All @@ -34,6 +34,8 @@ sd = __BRYTHON__.smartdown
def nameChanged():
sd.this.div.innerHTML = "<h4>Hello, %s</h4>" % sd.env.NAME
sd.smartdown.setVariable('NAME', '')
sd.this.dependOn = ['NAME']
sd.this.depend = nameChanged
```
Expand Down Expand Up @@ -229,6 +231,26 @@ sd.this.atExit(atExit)

Brython's default API is the `brython()` function which obtains Python3 source code from a `<script type="text/python3">` tag. Smartdown detects `brython` playables and generates a corresponding `<script ...>` tag. When the playable is *played*, the `Brython` compiler is invoked upon the target Python3 script and the translated Python is `eval`-ed in the context of a Smartdown-generated wrapper script that ensures that Smartdown's context is passed.

### Inter-language Communication

Because Smartdown's variables are available to all playables, whether Javascript or Brython, we can *compose* a document using multiple languages, depending on which language is appropriate and best expresses a concept. For example, we can write a Javascript playable that reacts to the same variable `NAME` as above. In this simple example, we'll just update the playable's DOM similar to how we did this in Brython above. For convenience, we have another input field below which will *reflect* and *affect* the value of the `NAME` variable.

[What is your Name (again)?](:?NAME)

```javascript/playable
const myDiv = this.div;
this.dependOn = ['NAME']
this.depend = function() {
const name = env.NAME;
myDiv.innerHTML = `<h4>Hello, ${name}</h4>`
};
if (!env.NAME) {
smartdown.setVariable('NAME', '');
}
```


#### What's next

Expand Down
56 changes: 52 additions & 4 deletions Extensions.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

## Extension Mechanisms for Smartdown

Smartdown tries to be extensible at several levels. One of these is the per-environment level that enables a teacher, for example, to create a topic-specific set of media and API urls (see `calc_handlers.js`, which is mostly undocumented right now).
Expand All @@ -18,7 +19,7 @@ We'll start with something simple, a Javascript library called [svgjs.com](http:


```javascript/playable/debug
//smartdown.use=https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.5/svg.min.js
//smartdown.import=https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.5/svg.min.js
// foo
Expand All @@ -34,7 +35,7 @@ Let's get a little fancier and embed a [VueJS](https://vuejs.org) app. We'll con


```javascript/playable
//smartdown.use=https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js
//smartdown.import=https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js
const innerDivId = this.div.id + '_inner';
Expand Down Expand Up @@ -76,8 +77,8 @@ this.depend = function() {
Here we'll specify that we want both the VueJS and the SVG.js libraries loaded. In this particular case, the order of loading doesn't matter. But Smartdown ensures that they are loaded sequentially (it is an optimization to load them in parallel).

```javascript/playable
//smartdown.use=https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.5/svg.min.js
//smartdown.use=https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js
//smartdown.import=https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.6.5/svg.min.js
//smartdown.import=https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js
const innerVueId = this.div.id + '_vue';
const innerSVGId = this.div.id + '_svg';
Expand Down Expand Up @@ -136,6 +137,53 @@ this.depend = function() {
```


### Including external playable sources

- Add a syntax to the current playable syntax that allows the body of the playable to contain an *indirection* or *reference* to an external text (usually, a `.js` file, but perhaps a `.graphviz` file).
- Possible syntax: //smartdown.include=./myBigScript.js
- Possible syntax: //#include './myBigScript.js'
- The interpretation of this will be that the script text will be asynchronously loaded at the time that the Smartdown text is compiled AND that the playable code displayed will be that from the included file.
- Optionally (OptionA), allow a playable to specify multiple included fragments which will be interpreted and displayed sequentially as the playable's code.
- Optionally (OptionB), display in the playable code BOTH the included code fragment (or fragments, if OptionA above is implemented) and the code that performed the inclusion. This might require fancier text control or just separate divs.

#### Simple JS Include

```javascript/playable
//smartdown.include=../gallery/ExtensionsPlayableHelloWorld.js
//
// This code will be ignored and never displayed in the playable, unless OptionB is implemented.
//
```

#### P5JS Ellipse Example (via include)


```p5js/playable
//smartdown.include=../gallery/ExtensionsPlayableP5.js
```


#### Error Testing

Let's see what happens when an included file does not exist.


```p5js/playable/debug
//smartdown.include=../gallery/NoSuchFile.js
```



#### GraphViz Example (via include)


```graphviz/playable
//smartdown.include=../gallery/ExtensionsPlayableGraphviz.gv
```



---


Expand Down
106 changes: 106 additions & 0 deletions ExtensionsPlayableGraphviz.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
digraph "unix" {
graph [ fontname = "Helvetica-Oblique",
fontsize = 36,
label = "\n\n\n\nObject Oriented Graphs\nStephen North, 3/19/93",
size = "12,12" ];
node [ shape = polygon,
sides = 4,
distortion = "0.0",
orientation = "0.0",
skew = "0.0",
color = white,
style = filled,
fontname = "Helvetica-Outline",
fontsize = "36"];
"5th Edition" [sides=9, distortion="0.936354", orientation=28, skew="-0.126818", color=salmon2];
"6th Edition" [sides=5, distortion="0.238792", orientation=11, skew="0.995935", color=deepskyblue];
"PWB 1.0" [sides=8, distortion="0.019636", orientation=79, skew="-0.440424", color=goldenrod2];
LSX [sides=9, distortion="-0.698271", orientation=22, skew="-0.195492", color=burlywood2];
"1 BSD" [sides=7, distortion="0.265084", orientation=26, skew="0.403659", color=gold1];
"Mini Unix" [distortion="0.039386", orientation=2, skew="-0.461120", color=greenyellow];
Wollongong [sides=5, distortion="0.228564", orientation=63, skew="-0.062846", color=darkseagreen];
Interdata [distortion="0.624013", orientation=56, skew="0.101396", color=dodgerblue1];
"Unix/TS 3.0" [sides=8, distortion="0.731383", orientation=43, skew="-0.824612", color=thistle2];
"PWB 2.0" [sides=6, distortion="0.592100", orientation=34, skew="-0.719269", color=darkolivegreen3];
"7th Edition" [sides=10, distortion="0.298417", orientation=65, skew="0.310367", color=chocolate];
"8th Edition" [distortion="-0.997093", orientation=50, skew="-0.061117", color=turquoise3];
"32V" [sides=7, distortion="0.878516", orientation=19, skew="0.592905", color=steelblue3];
V7M [sides=10, distortion="-0.960249", orientation=32, skew="0.460424", color=navy];
"Ultrix-11" [sides=10, distortion="-0.633186", orientation=10, skew="0.333125", color=darkseagreen4];
Xenix [sides=8, distortion="-0.337997", orientation=52, skew="-0.760726", color=coral];
"UniPlus+" [sides=7, distortion="0.788483", orientation=39, skew="-0.526284", color=darkolivegreen3];
"9th Edition" [sides=7, distortion="0.138690", orientation=55, skew="0.554049", color=coral3];
"2 BSD" [sides=7, distortion="-0.010661", orientation=84, skew="0.179249", color=blanchedalmond];
"2.8 BSD" [distortion="-0.239422", orientation=44, skew="0.053841", color=lightskyblue1];
"2.9 BSD" [distortion="-0.843381", orientation=70, skew="-0.601395", color=aquamarine2];
"3 BSD" [sides=10, distortion="0.251820", orientation=18, skew="-0.530618", color=lemonchiffon];
"4 BSD" [sides=5, distortion="-0.772300", orientation=24, skew="-0.028475", color=darkorange1];
"4.1 BSD" [distortion="-0.226170", orientation=38, skew="0.504053", color=lightyellow1];
"4.2 BSD" [sides=10, distortion="-0.807349", orientation=50, skew="-0.908842", color=darkorchid4];
"4.3 BSD" [sides=10, distortion="-0.030619", orientation=76, skew="0.985021", color=lemonchiffon2];
"Ultrix-32" [distortion="-0.644209", orientation=21, skew="0.307836", color=goldenrod3];
"PWB 1.2" [sides=7, distortion="0.640971", orientation=84, skew="-0.768455", color=cyan];
"USG 1.0" [distortion="0.758942", orientation=42, skew="0.039886", color=blue];
"CB Unix 1" [sides=9, distortion="-0.348692", orientation=42, skew="0.767058", color=firebrick];
"USG 2.0" [distortion="0.748625", orientation=74, skew="-0.647656", color=chartreuse4];
"CB Unix 2" [sides=10, distortion="0.851818", orientation=32, skew="-0.020120", color=greenyellow];
"CB Unix 3" [sides=10, distortion="0.992237", orientation=29, skew="0.256102", color=bisque4];
"Unix/TS++" [sides=6, distortion="0.545461", orientation=16, skew="0.313589", color=mistyrose2];
"PDP-11 Sys V" [sides=9, distortion="-0.267769", orientation=40, skew="0.271226", color=cadetblue1];
"USG 3.0" [distortion="-0.848455", orientation=44, skew="0.267152", color=bisque2];
"Unix/TS 1.0" [distortion="0.305594", orientation=75, skew="0.070516", color=orangered];
"TS 4.0" [sides=10, distortion="-0.641701", orientation=50, skew="-0.952502", color=crimson];
"System V.0" [sides=9, distortion="0.021556", orientation=26, skew="-0.729938", color=darkorange1];
"System V.2" [sides=6, distortion="0.985153", orientation=33, skew="-0.399752", color=darkolivegreen4];
"System V.3" [sides=7, distortion="-0.687574", orientation=58, skew="-0.180116", color=lightsteelblue1];
"5th Edition" -> "6th Edition";
"5th Edition" -> "PWB 1.0";
"6th Edition" -> LSX;
"6th Edition" -> "1 BSD";
"6th Edition" -> "Mini Unix";
"6th Edition" -> Wollongong;
"6th Edition" -> Interdata;
Interdata -> "Unix/TS 3.0";
Interdata -> "PWB 2.0";
Interdata -> "7th Edition";
"7th Edition" -> "8th Edition";
"7th Edition" -> "32V";
"7th Edition" -> V7M;
"7th Edition" -> "Ultrix-11";
"7th Edition" -> Xenix;
"7th Edition" -> "UniPlus+";
V7M -> "Ultrix-11";
"8th Edition" -> "9th Edition";
"1 BSD" -> "2 BSD";
"2 BSD" -> "2.8 BSD";
"2.8 BSD" -> "Ultrix-11";
"2.8 BSD" -> "2.9 BSD";
"32V" -> "3 BSD";
"3 BSD" -> "4 BSD";
"4 BSD" -> "4.1 BSD";
"4.1 BSD" -> "4.2 BSD";
"4.1 BSD" -> "2.8 BSD";
"4.1 BSD" -> "8th Edition";
"4.2 BSD" -> "4.3 BSD";
"4.2 BSD" -> "Ultrix-32";
"PWB 1.0" -> "PWB 1.2";
"PWB 1.0" -> "USG 1.0";
"PWB 1.2" -> "PWB 2.0";
"USG 1.0" -> "CB Unix 1";
"USG 1.0" -> "USG 2.0";
"CB Unix 1" -> "CB Unix 2";
"CB Unix 2" -> "CB Unix 3";
"CB Unix 3" -> "Unix/TS++";
"CB Unix 3" -> "PDP-11 Sys V";
"USG 2.0" -> "USG 3.0";
"USG 3.0" -> "Unix/TS 3.0";
"PWB 2.0" -> "Unix/TS 3.0";
"Unix/TS 1.0" -> "Unix/TS 3.0";
"Unix/TS 3.0" -> "TS 4.0";
"Unix/TS++" -> "TS 4.0";
"CB Unix 3" -> "TS 4.0";
"TS 4.0" -> "System V.0";
"System V.0" -> "System V.2";
"System V.2" -> "System V.3";
}

15 changes: 15 additions & 0 deletions ExtensionsPlayableHelloWorld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// This playable code has been loaded from ExtensionsPlayableHelloWorld.js
// in the same directory that its likely includer Extensions.md resides in.
//

console.log('W00t! This is ExtensionsPlayableHelloWorld.js');

const playableDiv = this.div;
playableDiv.style.border = '5px solid purple';
playableDiv.style.background = 'lightgreen';
playableDiv.innerHTML = '<h1>Hello World</h1>';

//
// End of ExtensionsPlayableHelloWorld.js
//
8 changes: 8 additions & 0 deletions ExtensionsPlayableP5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* global p5 */

p5.setup = function setup() {
};

p5.draw = function draw() {
p5.ellipse(50, 50, 80, 80);
};
2 changes: 1 addition & 1 deletion Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Use the navigation buttons below to explore different aspects of Smartdown. You
|[Audio and Video](:@Video)|[Stdlib](:@Stdlib)|[Tree](:@Tree)|[YouTube](:@YouTube)|
|[Gists](:@Gists)|[ThreeJS](:@Three)|[VectorTree](:@VectorTree)|[Extensions](:@Extensions)|
|[Cells](:@Cells)|[P5JS](:@P5JS)|[Dungeon](:@Dungeon)|[Brython](:@Brython)|
|[Multicards](:@Multicards)|[Mermaid](:@Mermaid)|[Dungeon3D](:@Games)|
|[Multicards](:@Multicards)|[Mermaid](:@Mermaid)|[Dungeon3D](:@Games)|[P5Play](:@P5Play)|

---

Expand Down
9 changes: 7 additions & 2 deletions Javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ smartdown.setVariable('COUNTER', counter, 'integer');

---

```javascript/playable/autoplay
The following code will adjust the size, border and background of the playable's `<div>` via the use of `this.div`, which points to the DOM element and permit normal DOM manipulation.

```javascript/playable
var playableDiv = this.div;
playableDiv.style.background='red';
playableDiv.style.border = '5px solid purple';
playableDiv.style.background='lightgreen';
playableDiv.innerHTML = '<h1>Hello World</h1>';
```

---
Expand Down
2 changes: 1 addition & 1 deletion LDF.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Exploring the possibilities of [Linked Data Fragments](http://linkeddatafragment
[Query](:!query|code)

```javascript/playable/autoplay
//smartdown.use=ldf
//smartdown.import=ldf
var ldf = smartdown.ldf;
smartdown.ldf.Logger.setLevel('NOTICE');
Expand Down
2 changes: 1 addition & 1 deletion Mandelbrot.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This is a one-pint demo of how ThreeJS and Smartdown can be used to understand t
[Set X/Y/Zoom](:=useCoordinates=1) [](:?coordinates)

```javascript/playable/autoplay
//smartdown.use=three
//smartdown.import=three
var vertexShader =
`
precision highp float;
Expand Down
2 changes: 1 addition & 1 deletion P5JS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var song;
p5.preload = function () {
// Load a sound file
song = p5.loadSound(p5.exampleSound);
song = p5.loadSound('https://unpkg.com/smartdown-gallery/resources/Damscray_DancingTiger.mp3');
song.owner = p5;
};
Expand Down
65 changes: 65 additions & 0 deletions P5Play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## p5.play Examples

[p5.play](https://github.com/molleindustria/p5.play) is a Javascript library that extends the functionality of p5.js with animation, sprites, and collision detection, amongst other features. We will be using the [code.org fork](https://github.com/code-dot-org/p5.play) of p5.play, because it appears to be more maintained currently.

The following example From [Making Games with p5.play](https://creative-coding.decontextualize.com/making-games-with-p5-play/) uses `smartdown.import` to dynamically load the p5.play library, and then it draws a field of 'coins' which you can gather up by moving your mouse around.

**Warning** This use of p5.play is at a very early stage, and there are negative interactions between the p5.play library and ordinary p5.js code. For example, p5.play modifies the p5.camera function and turns it into an object. This is not well-behaved and results in other p5.js examples (e.g., Mobius) not working. The workaround for now is to NOT run the p5.play examples except for experimental purposes, and if you do run a p5.play example, remember to reload your Smartdown page to eliminate the negative effects on your p5.js library. This is a fixable problem, but requires more study.


```P5JS/playable/debug
//smartdown.import=https://cdn.rawgit.com/code-dot-org/p5.play/master/lib/p5.play.js
window.allowP5PlayInit = false;
var Group = p5.Group;
var createSprite = p5.createSprite.bind(p5);
var drawSprites = p5.drawSprites.bind(p5);
var coins;
var player;
var score = 0;
function setup() {
createCanvas(400, 400);
coins = new Group();
for (var i = 0; i < 10; i++) {
var c = createSprite(
random(100, width-100),
random(100, height-100),
10, 10);
c.shapeColor = color(255, 255, 0);
coins.add(c);
}
player = createSprite(50, 50, 40, 40);
player.shapeColor = color(255);
}
function draw() {
background(50);
player.velocity.x =
(mouseX-player.position.x)*0.1;
player.velocity.y =
(mouseY-player.position.y)*0.1;
player.overlap(coins, getCoin);
drawSprites();
fill(255);
noStroke();
textSize(72);
textAlign(CENTER, CENTER);
if (coins.length > 0) {
text(score, width/2, height/2);
}
else {
text("you win!", width/2, height/2);
}
}
function getCoin(player, coin) {
coin.remove();
score += 1;
}
```


---

[Back to Home](:@Home)

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# gallery
# smartdown-gallery

Example Smartdown documents and associated resources that demonstrate various Smartdown features and serve as raw material for other Smartdown demos.

Expand All @@ -14,3 +14,6 @@ See [Smartdown README](https://smartdown.site/lib/#README) for more info.
- **0.0.4** Updates examples to support Smartdown 0.0.76 and dynamic imports, D3v5, P5JS Global syntax, and more.
- **0.0.5** Updates examples to support SD 0.0.77
- **0.0.6** Adds smartdown.use Externals examples. Adds Brython examples. Removes duplicate John Gruber quotation.
- **0.0.7** Extend Brython example to show integration with JS. Add smartdown.include examples to Extensions.md. Change smartdown.use to smartdown.import


Loading

0 comments on commit b828210

Please sign in to comment.