The Larch - a visual interactive programming environment

123
A VISUAL INTERACTIVE PROGRAMMING ENVIRONMENT FOR PYTHON http://sites.google.com/site/larchenv G. FRENCH J. R. KENNAWAY A. M. DAY PYCON IRELAND 2011 The Larch Environment Image by alcomm, flickr.com

description

The Larch Environment is a visual interactive programming environment for Jython/Python, that makes programming more visual. Its is designed for the creation of visual interactive programs, and programs that operate as interactive technical literature. To this end, protocols for presenting objects visually have been devised. An active document based programming environment builds on the edit-run-debug cycle of a standard console, allowing a programmer to experiment with ideas, and develop visual programs at the same time. Additionally, a way of embellishing source code with visual content is presented.http://sites.google.com/site/larchenv

Transcript of The Larch - a visual interactive programming environment

Page 1: The Larch - a visual interactive programming environment

A VISUAL INTERACTIVE PROGRAMMING ENVIRONMENT FOR PYTHONhttp://sites.google.com/site/larchenv

G. FRENCHJ. R. KENNAWAY

A. M. DAYPYCON IRELAND 2011

The Larch Environment

Image by alcomm, flickr.com

Page 2: The Larch - a visual interactive programming environment

Motivation

Page 3: The Larch - a visual interactive programming environment

We look at three problems

Page 4: The Larch - a visual interactive programming environment

Textual output in a Python console can be difficult to understand(DEMO)

Page 5: The Larch - a visual interactive programming environment

Source code in a text editor is not interactive enoughConsoles are only good for a few lines at

a time

Page 6: The Larch - a visual interactive programming environment

Textual source code can be difficult to understand and comprehend(think the vertices

of a polygon in code form)

Page 7: The Larch - a visual interactive programming environment

What is‘The Larch Environment’?

Page 8: The Larch - a visual interactive programming environment

A visual interactive programming environment

Page 9: The Larch - a visual interactive programming environment

The goal is:To make programming more visual

Page 10: The Larch - a visual interactive programming environment

How do we do that?

• Visual object presentation• Programming environment

o Visual consoleo Worksheets

• Visual programming via embedded objects

Page 11: The Larch - a visual interactive programming environment

Visual object presentation

Page 12: The Larch - a visual interactive programming environment

“Pictures are pretty”DEMOVisual console

Page 13: The Larch - a visual interactive programming environment

What design pattern do we commonly use for developing GUI apps?

Page 14: The Larch - a visual interactive programming environment

Model

View Controller

MVC Architecture

Page 15: The Larch - a visual interactive programming environment

MVC requires:Model class, View class, Controller class

Page 16: The Larch - a visual interactive programming environment

Our approach:Type coercionType coercion: coerce an object to another type.

Page 17: The Larch - a visual interactive programming environment

Type coercion used throughout Python

Page 18: The Larch - a visual interactive programming environment

Examples:repr(), str()__index__()etc

Page 19: The Larch - a visual interactive programming environment

Its simple

Page 20: The Larch - a visual interactive programming environment

The Larch Environment:Use type coercion for visual presentation

Page 21: The Larch - a visual interactive programming environment

Coerce objects to something visual(a Pres)

Page 22: The Larch - a visual interactive programming environment

HOWTO:The simplified version

Page 23: The Larch - a visual interactive programming environment

Define the following method:def __present__(self, fragment, inherited_state)

Page 24: The Larch - a visual interactive programming environment

For Java objects:Implement Presentable interface

Page 25: The Larch - a visual interactive programming environment

Presentations constructed using a combinatorial API

Page 26: The Larch - a visual interactive programming environment

Label( ‘Hello’ ) Hello

Button.buttonWithLabel( ‘Button’ ) Button

a = Label( ‘A’ )b = Label( ‘B’ )c = Label( ‘C’ )d = Label( ‘D’ )

Row( [ a, b, c, d ] ) ABCD

Column( [ a, b, c, d ] ) ABCD

Page 27: The Larch - a visual interactive programming environment

Presentation combinators:Many moreFlow layouts, mathematical fractions, superscriptRich text, other utilitiesWrite your own by combining existing ones!P.S. Appearance controlled with

style sheets

Page 28: The Larch - a visual interactive programming environment

“Type coercion is easy”DEMO:__present__()

Page 29: The Larch - a visual interactive programming environment

Can also handle objects in the Java or Python standard libraries

Page 30: The Larch - a visual interactive programming environment

Create an ‘object presenter’.Register it for the relevant class.When asked to present an

instance of the class, Larch finds the appropriate presenter and uses it.(no monkeypatching

required)

Page 31: The Larch - a visual interactive programming environment

Thats how the images were shown;they are java.awt.BufferedImage objects

Page 32: The Larch - a visual interactive programming environment

Perspectives

Page 33: The Larch - a visual interactive programming environment

Different perspectives present an object in different ways

Page 34: The Larch - a visual interactive programming environment

Like different views in MVC

Page 35: The Larch - a visual interactive programming environment

The one I have talked about (__present__, Presentable, etc) is the ‘Default perspective’

Page 36: The Larch - a visual interactive programming environment

There are other perspectives

Page 37: The Larch - a visual interactive programming environment

E.g. The inspector perspectives

Page 38: The Larch - a visual interactive programming environment

“Visual Introspection”DEMO: INSPECTOR PERSPECTIVE

Page 39: The Larch - a visual interactive programming environment

Programming Environment-Visual console

Page 40: The Larch - a visual interactive programming environment

You’ve seen most of it

Page 41: The Larch - a visual interactive programming environment

So lets look at some of the more ‘esoteric’ features

Page 42: The Larch - a visual interactive programming environment

Model dragging

Page 43: The Larch - a visual interactive programming environment

Everything in Larch is an object being presented (via type coercion)

Page 44: The Larch - a visual interactive programming environment

The home pageProjectsThe console itself!

Page 45: The Larch - a visual interactive programming environment

What if we want to manipulate an object that we can see?

Page 46: The Larch - a visual interactive programming environment

CTRL+ALT +drag it!

Page 47: The Larch - a visual interactive programming environment

“I see something: how does it work?”DEMO: inspect a project

Page 48: The Larch - a visual interactive programming environment

An interesting side fact!

Page 49: The Larch - a visual interactive programming environment

Our source code editor does not edit text

Page 50: The Larch - a visual interactive programming environment

Its a structured editorCode is represented as an abstract syntax tree (AST)

Page 51: The Larch - a visual interactive programming environment

A perspective is used to present is as something that looks and behaves (mostly) like text

Page 52: The Larch - a visual interactive programming environment

It means our code is in tree form

Page 53: The Larch - a visual interactive programming environment

We can write our own refactorings!

Page 54: The Larch - a visual interactive programming environment

“Change your code fast!”DEMO:Refactoring

Page 55: The Larch - a visual interactive programming environment

Programming Environment-Worksheets

Page 56: The Larch - a visual interactive programming environment

Interactive consoles are great.Caveat: gets difficult when working with more than a few lines

of code at a timeE.g. Whole modules

Page 57: The Larch - a visual interactive programming environment

For complete programs we turn to a text editorWe lose the interactivity

Page 58: The Larch - a visual interactive programming environment

What if we could blend the two?(and add rich text to boot)

Page 59: The Larch - a visual interactive programming environment

“Python modules. With pictures.”DEMO: WORKSHEET(with cellular automata)

Page 60: The Larch - a visual interactive programming environment

Worksheets are active documents

Page 61: The Larch - a visual interactive programming environment

Act as modulesCan import code from other worksheets within the project

Page 62: The Larch - a visual interactive programming environment

You can divide your module code into a number of blocks

Page 63: The Larch - a visual interactive programming environment

Each block can show a result – a step along the path of a computation

Page 64: The Larch - a visual interactive programming environment

To refresh results: hit F5

Page 65: The Larch - a visual interactive programming environment

Rapid Edit-Run-Debug cycle:Alter codeF5Repeat

Page 66: The Larch - a visual interactive programming environment

“Code faster!”DEMO: Edit-Run-Debug cycle(cellular automata)

Page 67: The Larch - a visual interactive programming environment

Visual Programming

Page 68: The Larch - a visual interactive programming environment

Quite a rich history in the research community

Page 69: The Larch - a visual interactive programming environment

Circuit diagrams (LabView), data-flow diagrams, draggable tiles (Scratch, Squeak tile view), etc

Page 70: The Larch - a visual interactive programming environment

Nice for small simple programs

Page 71: The Larch - a visual interactive programming environment

Large programs look like rat’s nestsNot practical

Page 72: The Larch - a visual interactive programming environment

Text remains the dominant medium for source code

Page 73: The Larch - a visual interactive programming environment

Diagrams are still appropriate in certain circumstances

Page 74: The Larch - a visual interactive programming environment

Lets use diagrams (or visual layout) where we need them!

Page 75: The Larch - a visual interactive programming environment

“Play God.”DEMO: OrrerySub-demos:Table editorsEmbedded table

Page 76: The Larch - a visual interactive programming environment

“Drawings. Inside code.”DEMO: Polygon

Page 77: The Larch - a visual interactive programming environment

Embedded objects can use a protocol to customise their behaviour

Page 78: The Larch - a visual interactive programming environment

__py_eval__ Act like an expression - return the result of evaluating

__py_evalmodel__ Act like an expression - return an AST

__py_exec__ Act like a statement – called at runtime

__py_execmodel__ Act like a statement – return an AST

Page 79: The Larch - a visual interactive programming environment

AST GenerationWhat does this sound like?

Page 80: The Larch - a visual interactive programming environment

AST Generation~=Visual LISP macros

Page 81: The Larch - a visual interactive programming environment

Crosses compile-time / run-time barrier

Page 82: The Larch - a visual interactive programming environment

Compile-time (edit-time) objects available at run time

Page 83: The Larch - a visual interactive programming environment

Run-time objects / values can modify or be modified by compile-time objects

Page 84: The Larch - a visual interactive programming environment

“LISPy Smalltalky goodness”DEMO: LZW compressor

Page 85: The Larch - a visual interactive programming environment

Literate programming style extensions

Page 86: The Larch - a visual interactive programming environment

Literate programmingRefresher

Page 87: The Larch - a visual interactive programming environment

Invented by Knuthcirca 1984

Page 88: The Larch - a visual interactive programming environment

Basic idea

Page 89: The Larch - a visual interactive programming environment

Present programs as documents suitable for reading by people

Page 90: The Larch - a visual interactive programming environment

Break a program into sections, in the same way you do for a book

Page 91: The Larch - a visual interactive programming environment

Each section consists of a natural language description, and the code that implements it

Page 92: The Larch - a visual interactive programming environment

Present sections in the order of ‘human logic’*, not the order of the compiler

* Wikipedia article on Literate Programming

Page 93: The Larch - a visual interactive programming environment

Use the ‘weave’ tool to generate the human readable document, typeset with LATEX

Page 94: The Larch - a visual interactive programming environment

Use the ‘tangle’ tool to generate compile code for compiler

Page 95: The Larch - a visual interactive programming environment

Use the ‘tangle’ tool to generate compile code for compiler

Page 96: The Larch - a visual interactive programming environment

Our approach

Page 97: The Larch - a visual interactive programming environment

Use embedded objects to represent sections(Python expressions and suites)

Page 98: The Larch - a visual interactive programming environment

Allow them to be references/used in multiple placed throughout the document

Page 99: The Larch - a visual interactive programming environment

“Interactive pretty docuemnts”DEMO: Literate extensions

Page 100: The Larch - a visual interactive programming environment

Conclusions

Page 101: The Larch - a visual interactive programming environment

Visual object presentation by type-coercion

Page 102: The Larch - a visual interactive programming environment

Encourages a functional approach to UI composition Handling state changes:Re-create,

DON’T MUTATE

Page 103: The Larch - a visual interactive programming environment

Visual representation of values is a BIG EPIC WINEven if you use only visual cues (e.g.

borders around text)

Page 104: The Larch - a visual interactive programming environment

Worksheets are active documents. They also expand on the rapid edit-run cycle of the

console

Page 105: The Larch - a visual interactive programming environment

Allow for rapid development of visual interactive applications

Page 106: The Larch - a visual interactive programming environment

Visual programming by embedded objects

Page 107: The Larch - a visual interactive programming environment

Visual programmingwhere you need it

Page 108: The Larch - a visual interactive programming environment

Allows you to visually extend the syntax of the language

Page 109: The Larch - a visual interactive programming environment

No need to alter the compiler – its just embedded object references

Page 110: The Larch - a visual interactive programming environment

References to objects you implementyourself

Page 111: The Larch - a visual interactive programming environment

Embedded object referencesCan cross compile-time / run-time barrier

Page 112: The Larch - a visual interactive programming environment

LISPy / Smalltalky stuffIN PYTHON

Page 113: The Larch - a visual interactive programming environment

PROJECT STATUS

Page 114: The Larch - a visual interactive programming environment

Research Prototype(not ready for real use )

Page 115: The Larch - a visual interactive programming environment

TODOs:DocumentationBug fixesToo much more........

Page 116: The Larch - a visual interactive programming environment

Related Work

Page 117: The Larch - a visual interactive programming environment

MathematicaMathematica CDFIntentional Software Domain WorkbenchJetbrains Meta

Programming System

Page 118: The Larch - a visual interactive programming environment

Acknowledgements

Page 119: The Larch - a visual interactive programming environment

Academic supervisory team

Dr. Richard KennawayProf. Andy Day

University of East Anglia, Norwich, UK

Page 120: The Larch - a visual interactive programming environment

The Jython team

Developing The Larch would have been very difficult without Jython

Page 121: The Larch - a visual interactive programming environment

IF TIME ALLOWS:DEMO: KD-Tree

Page 122: The Larch - a visual interactive programming environment

IF TIME ALLOWS:DEMO: SIMPLE COMPILER

Page 123: The Larch - a visual interactive programming environment

G. FRENCHJ. R. KENNAWAY

1. M. DAYhttp://sites.google.com/site/larchenv

THANK YOU!

Image by alcomm, flickr.com