Skip to content

Instantly share code, notes, and snippets.

@ptmcg
Last active May 28, 2022 03:07
Show Gist options
  • Save ptmcg/09b5be0cb3623a478851100488788821 to your computer and use it in GitHub Desktop.
Save ptmcg/09b5be0cb3623a478851100488788821 to your computer and use it in GitHub Desktop.
PyScript runs pyparsing code to generate railroad diagram
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- pyparsing==3.0.9
- railroad-diagrams
- jinja2
</py-env>
<py-script>
# force reload of latest pyparsing
import pyparsing
import importlib
importlib.reload(pyparsing)
</py-script>
</head>
<body>
<table cellpadding=30>
<tr>
<td>
<pre>
import pyparsing as pp
integer = pp.Word(pp.nums)
word = pp.Word(pp.alphas, pp.alphas+".")
street_name = pp.Group(word[1, ...])
street_address = (integer + street_name)
pp.autoname_elements()
street_address.create_diagram(output_html="diag.html")
print(street_address.parse_string("123 Main St."))
</pre>
</td>
<td>
<py-script>
import io
import pyparsing as pp
integer = pp.Word(pp.nums)
word = pp.Word(pp.alphas, pp.alphas+".")
street_name = pp.Group(word[1, ...])
street_address = (integer + street_name)
pp.autoname_elements()
diag_html = io.StringIO()
street_address.create_diagram(output_html=diag_html)
print(diag_html.getvalue())
</py-script>
</td>
</tr>
<tr>
<td/>
<td>
<py-script>
print(street_address.parse_string("123 Main St."))
</py-script>
</td>
</tr>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment