File: 03-template.py

package info (click to toggle)
python-pattern 2.6%2Bgit20150109-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,672 kB
  • sloc: python: 53,865; xml: 11,965; ansic: 2,318; makefile: 94
file content (48 lines) | stat: -rw-r--r-- 1,215 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os, sys; sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

from pattern.graph import Graph, CSS, CANVAS

# This example demonstrates how to roll dynamic HTML graphs.
# We have a HTML template in which content is inserted on-the-fly.

# This is useful if the graph data changes dynamically, 
# e.g., the user clicks on a node and is taken to a webpage with a new subgraph.

template = '''
<!doctype html> 
<html>
<head>
\t<meta charset="utf-8">
\t<script type="text/javascript" src="canvas.js"></script>
\t<script type="text/javascript" src="graph.js"></script>
\t<style type="text/css">
\t\t%s
\t</style>
</head>
<body> 
\t%s
</body>
</html>
'''.strip()

def webpage(graph, **kwargs):
    s1 = graph.serialize(CSS, **kwargs)
    s2 = graph.serialize(CANVAS, **kwargs)
    return template % (
        s1.replace("\n", "\n\t\t"),
        s2.replace("\n", "\n\t")
    )

# Create a graph:
g = Graph()
g.add_node("cat")
g.add_node("dog")
g.add_edge("cat", "dog")

# To make this work as a cgi-bin script, uncomment the following lines:
##!/usr/bin/env python
#import cgi
#import cgitb; cgitb.enable() # Debug mode.
#print "Content-type: text/html"

print webpage(g, width=500, height=500)