memory-viz
v0.8.0
Published
Library for creating beginner-friendly memory model diagrams.
Readme
MemoryViz: Creating memory model diagrams
MemoryViz is a visualization tool that generates memory model diagrams for Python code, aimed at students and educators. MemoryViz is written in Javascript and is built on top of the Rough.js library.
For more information, check out our demo and project documentation.
Installation
Install MemoryViz using npm (requires Node.js to be installed):
$ npm install memory-vizUsage
MemoryViz can be run from the command line or using its Javascript API.
Command-line interface
Given a JSON file demo.json that encodes a state of Python memory and some styling options:
[
{
"type": ".frame",
"name": "__main__",
"id": null,
"value": { "lst1": 82, "lst2": 84, "p": 99, "d": 10, "t": 11 }
},
{
"type": "str",
"id": 19,
"value": "David is cool!",
"style": ["highlight"]
},
{
"type": "int",
"id": 13,
"value": 7
}
]you can run the following command in the terminal:
$ npx memory-viz --output demo_output.svg demo.jsonThis producs an SVG file, demo_output.svg, that visualizes the state of memory:
Javascript API (Node.js)
Call the draw function on an array that encodes a state of Python memory and some styling options.
Here's a complete example, which produces the same SVG output as above.
import { draw } from "memory-viz";
const objects = [
{
type: ".frame",
name: "__main__",
id: null,
value: { lst1: 82, lst2: 84, p: 99, d: 10, t: 11 },
},
{
type: "str",
id: 19,
value: "David is cool!",
style: ["highlight"],
},
{
type: "int",
id: 13,
value: 7,
},
];
const model = draw(objects, true, { width: 1300 });
model.save("demo_output.svg");Javascript API (browser)
MemoryViz can also be run in the browser by loading the library from a CDN (e.g., jsDelivr) and accessing the global memoryViz object.
Here is a standalone example.
Contributing
Installation
Install Node.js.
Clone the MemoryViz repository and
cdinto it:$ git clone https://github.com/david-yz-liu/memory-viz.git $ cd memory-vizInstall the dependencies:
$ npm installInstall the pre-commit hooks to automatically format your code when you make commits:
$ npx husky initCompile the MemoryViz library:
$ npm run build-dev --workspace=memory-vizRun the test suite to check that all tests pass:
$ npm test
Developer tips
Automatic Javascript compilation. Rather than running npm run build-dev to recompile your Javascript bundle every time you make a change, you can instead run the following command:
$ npm run watch --workspace=memory-vizThis will use webpack to watch for changes to the Javascript source files and recompile them automatically.
Note: this command will keep running until you manually terminate it (Ctrl + C), and so you'll need to open a new terminal window to enter new terminal commands like running the demo below.
Viewing Jest SVG snapshots. To easily view the Jest SVG snapshots, open the file memory-viz/src/tests/__snapshots__/snapshot.html and select the snapshot outputs with the *.snap extension.
