Skip to content

Instantly share code, notes, and snippets.

@ianfun
Last active May 9, 2022 07:37
Show Gist options
  • Save ianfun/b2cabb1cfdc0810b6275e959243aa23d to your computer and use it in GitHub Desktop.
Save ianfun/b2cabb1cfdc0810b6275e959243aa23d to your computer and use it in GitHub Desktop.
simple python module browser
def show_m(path):
from tkinter import Text, Tk
from idlelib.colorizer import color_config, ColorDelegator
root = Tk()
root.title(path)
text = Text(root)
text.pack(expand=1, fill="both")
with open(path, encoding='utf-8') as f:
text.insert("insert", f.read())
text.focus_set()
from idlelib.percolator import Percolator
color_config(text)
p = Percolator(text)
d = ColorDelegator()
p.insertfilter(d)
root.focus()
root.mainloop()
from sys import argv
show_m(argv[1])
import inspect
import pkgutil
import sys
import webbrowser
from http.server import *
from subprocess import Popen
font = r'''<!DOCTYPE html>
<html>
<head><meta charset="utf-8">
<title>pydoc</title>
<script type="text/javascript">
window.onload = function(){
document.getElementById('index').childNodes.forEach(function(e){
var a=document.createElement('a');
a.innerText=e.getAttribute("id").slice(1);
a.setAttribute("href", e.getAttribute("id"));
e.appendChild(a);
})
}
function read() {
if (document.getElementById('path')==undefined){
alert("not a source based module");
return;
}
var x=new XMLHttpRequest();
x.open("POST", "http://localhost:8000", true);
x.onreadystatechange=function(){
if (x.readyState == 4 && x.status == 200){
if (x.response!='success'){
alert("fail to call idle");
}
}
}
x.send(document.getElementById('path').innerText);
}
</script>
</head>
<body>
<button onclick="window.open('https://docs.python.org/3/library/'+document.getElementById('name').innerText+'.html')">see doc</button>
<button onclick="read()">source</button>
<style type="text/css">
body {
background-color: #25302F;
}
pre {
font-family: Consolas;
}
pre.cal {
color: blue;
}
pre.ano {
font-size: 10px;
color: green;
}
div.var {
background-color: pink;
margin : 30px;
}
</style>'''
end = '</body></html>'
rt = '''<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>module browser</title>
<script type="text/javascript">window.onload=function(){document.getElementById('m').childNodes.forEach(function(e){e.setAttribute("href", e.innerText+'.html');})}</script></head>
<body><pre>%s</pre><div id="m"><a>''' % str(sys.path) + '</a><br/><a>'.join(sys.modules) + '</a>'
try:
for x, y, z in pkgutil.walk_packages(onerror=None):
rt += '<a>%s</a><br/>' % y
except:
pass
rt += "</div></body></html>"
with open("modules.html", 'w', encoding='utf-8') as w:
w.write(rt)
def getdoc(path):
b = font
path = path[1::][0:-5] #\path.html
try:
exec("import " + path)
module = eval(path)
b += "<big id='name'>%s</big>" % path
try:
b += "<p id='path'>%s</p>" % module.__file__
except AttributeError:
b += "<p>(built-in)</p>"
if module.__doc__ is None:
b += '<ul id="index"><li id="#'
else:
b += '<pre class="ano">' + module.__doc__ + '</pre><ul id="index"><li id="#'
b += '"></li><li id="#'.join(dir(module)) + '"></li></ul><div>'
for i in dir(module):
thing = getattr(module, i)
b += '</div><div class="var" id=%s>' % i
if inspect.isfunction(thing):
b += '<span>function</span><pre class="cal">%s</pre><pre class="ano">%s' % (i, thing.__doc__ if thing.__doc__ else "\n")
elif inspect.isclass(thing):
b += '<span>class</span><pre class="cal">%s</pre><pre class="ano">%s' % (i, thing.__doc__ if thing.__doc__ else "\n")
else:
b += '<pre class="var">%s: %s=%s</pre>' % (i, thing.__class__.__name__, str(thing))
b += '</div>'
return (b + end).encode('utf-8')
except Exception as e:
return (b + repr(e) + end).encode('utf-8')
class backend(BaseHTTPRequestHandler):
def log_message(*args, **kwargs): ...
def log_request(*args, **kwargs): ...
def do_POST(self):
"""open idle and show module source.
called by javascript"""
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
Popen([sys.executable, 'see.py', self.rfile.read(int(self.headers['Content-Length'])).decode('utf-8')])
self.wfile.write(b"success")
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html; charset=UTF-8')
self.end_headers()
if self.path.endswith('html'):
self.wfile.write(getdoc(self.path))
else:
with open("modules.html", 'rb') as rb:
for i in rb:
self.wfile.write(rb)
with HTTPServer(("", 8000), backend) as s:
print("http://localhost:8000/")
webbrowser.open("http://localhost:8000/")
s.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment