Library for creating beginner-friendly memory model diagrams.
npm install memory-vizMemoryViz 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.
Install MemoryViz using npm (requires Node.js to be installed):
``bash`
$ npm install memory-viz
MemoryViz can be run from the command line or using its Javascript API.
Given a JSON file demo.json that encodes a state of Python memory and some styling options:
`json`
[
{
"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:
`bash`
$ npx memory-viz --output demo_output.svg demo.json
This producs an SVG file, demo_output.svg, that visualizes the state of memory:
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.
`js
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");
`
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.
1. Install Node.js.
2. Clone the MemoryViz repository and cd into it:
`bash`
$ git clone https://github.com/david-yz-liu/memory-viz.git
$ cd memory-viz
3. Install the dependencies:
`bash`
$ npm install
4. Install the pre-commit hooks to automatically format your code when you make commits:
`bash`
$ npx husky init
5. Compile the MemoryViz library:
`bash`
$ npm run build-dev --workspace=memory-viz
6. Run the test suite to check that all tests pass:
`bash`
$ npm test
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:
`bash`
$ npm run watch --workspace=memory-viz
This 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.