Thanks! That does solve the import restricted dependency.
The import globals for the Response() object is still an issue.
I tried fiddling with my copy to build a mock Response() object if we
can't import globals.
This is what I have so far:
gluon/template.py | line 867
~~~~~
# Here to avoid circular Imports
try:
from globals import Response
except:
import cStringIO
from xml.sax.saxutils import escape, quoteattr
class Response():
def __init__(self):
self.body = cStringIO.StringIO()
def write(self, data, escape=True):
if not escape:
self.body.write(str(data))
elif hasattr(data,'xml') and callable(data.xml):
self.body.write(data.xml())
else:
# otherwise, make it a string
if not isinstance(data, (str, unicode)):
data = str(data)
elif isinstance(data, unicode):
data = data.encode('utf8', 'xmlcharrefreplace')
self.body.write(data)
~~~~~
I was planning to escape the data with the escape and quoteattr
provided from xml.sax.saxutils, but I wasn't successful with that, so
I left it out for now.
Here's my code snippet:
nixie/util/text.py | line 19
~~~~~
import os, subprocess, paths, template
def render(inFile):
content = pandoc(str(inFile))
templateFile = os.path.join(paths.get_prog_root(), 'templates', 'view.html')
styles = []
styles.append(os.path.join(paths.get_prog_root(), 'css', 'style.css'))
return template.render(
filename=templateFile,
context=dict(content=content, styles=styles)
)
~~~~~
templates/view.html
~~~~~
<html>
<head>
{{for css in styles:}}
<link rel="stylesheet" href="{{=css}}" type="text/css" />
{{pass}}
</head>
<body>
{{=content}}
</body>
</html>
~~~~~
When I run this, I get an error message that doesn't really help me
much. Here's the output:
~~~~~
C:\projects\nixie>c:\Python26\python.exe Nixie.py README.txt
Traceback (most recent call last):
File "C:\projects\nixie\nixie\qt\NixieAccessManager.py", line 41, in
createRequest
reply = NixieReply(request.url(), self.GetOperation, parent=self)
File "C:\projects\nixie\nixie\qt\NixieReply.py", line 30, in __init__
self.content = text.render(url.toLocalFile())
File "C:\projects\nixie\nixie\util\text.py", line 22, in render
content = pandoc(str(inFile))
File "C:\projects\nixie\nixie\util\text.py", line 63, in pandoc
cwd = cwd
File "c:\Python26\lib\subprocess.py", line 623, in __init__
errread, errwrite)
File "c:\Python26\lib\subprocess.py", line 833, in _execute_child
startupinfo)
WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect
~~~~~
Although it looks like pandoc(str(inFile)) might be the culprit from
the stack trace, if I just use the output from pandoc(str(inFile)),
everything works fine, so I doubt that this is the cause.
I really appreciate your help. I've started trying Pandoc
(http://johnmacfarlane.net/pandoc/) instead of the python-markdown
module, and I noticed that Pandoc comes with it's own template system.
So, it may make more sense for me to use Pandoc's templates instead,
if I decide to go with it.
On Sun, Jun 5, 2011 at 10:45 PM, Massimo Di Pierro
<[email protected]> wrote:
> check trunk. I removed it. I am sure we can do better.
>
> On Jun 5, 2011, at 9:26 PM, Ryan Seto wrote:
>
>> Thank you very much for your prompt response.
>>
>> It looks like the file gluon/template.py does pull in some extra
>> dependencies, however.
>>
>> It tries to import restricted on line 20 and import globals on line 863.
>>
>> The restricted module dependency may be easy to remove, since it
>> appears that it only uses it for raising exceptions. However, it
>> looks like the Response object is used from the globals module.
>>
>> On Sun, Jun 5, 2011 at 9:12 PM, Massimo Di Pierro
>> <[email protected]> wrote:
>>>
>>>
>>> On Jun 4, 7:58 pm, Ryan Seto <[email protected]> wrote:
>>>> I really like how elegant and simple it is to create views in web2py.
>>>> Would it be possible to use the view/template engine in a standalone
>>>> application?
>>>
>>> yes.
>>>
>>> you only need the file gluon/template.py
>>>
>>> look at the example inside. You only the render function.
>>>
>>>>
>>>> I'm writing a desktop application to view formatted text, like
>>>> markdown, using PyQT's QtWebKit to render the generated html, and I
>>>> would like to integrate web2py's method for generating views into my
>>>> project.
>>>>
>>>> I've been looking through web2py's source and the mailing list, and it
>>>> seems that response.render( view_text, dict() ) might be the closest
>>>> thing to what I'm looking for. However, it looks like there's a lot
>>>> of dependencies wrapped around it and the objects weren't made to be
>>>> used in the context of another application.
>>>>
>>>> If this is the case, would it make sense to compartmentalize the parts
>>>> for rendering a view into it's own module so they can be used in a
>>>> standalone application, similar to the dal? I would be willing to
>>>> come up with a patch for this, if I could get some hints on where to
>>>> start.
>
>