2

I'm writing an app to selectively list files in a directory. I'd like users to open an HTML file and get a message "Updating..." while Python walks through the directory and collects the information. When python finishes, the page would be populated with the list of files.

Is this possible with Jinja2? Reading up on it, it looks like many people use it generate HTML files from Python. I'd like to do something slightly different by using it to call Python within the HTML. Or maybe there's another approach I should be taking? Any advise would be appreciated.

1
  • 1
    You can't execute Python code from HTML. To do this sort of thing, you must a run a server. Commented May 31, 2013 at 11:39

3 Answers 3

3

You can't execute Python from the rendered template, but you can do what you want by combining Python with Javascript. I'm going to use Flask as an example web framework but it should be pretty clear what it does even if you're not familiar with Flask in particular.

The idea is that you have two routes/addresses. One serves you the basic view where you want your "Updating..." thing to take place. The other route returns an HTML fragment that consists of the directory data rendered to a suitable HTML presentation. So something like this:

Python:

def _walk_through_directories():
    """
    Walk through directories and gather the
    data you want and return it in some collection
    for example.
    """
    ...
    return directory_data


@app.route('/directories')
def directories():
    return render_template('directories.html')


@app.route('/directories/fragment')
def directories():
    directory_data = _walk_through_directories()
    return render_template('directories_fragment.html',
                           directory_data=directory_data)

Now when the user enters your basic page at /directories, he/she sees your basic template that does not yet contain anything about the directory data. To remedy this, you can execute Javascript after the document is done loading and fetch the content by doing a HTTP GET with Ajax. So something like this:

Javascript + jQuery called from "directories.html":

$('#message-box').html('Updating...');

$.get('/directories/fragment', function(data) {
  $('#result-box').html(data);
  $('#message-box').html('All done!');
});

So the idea is to serve the basic page with Python and then use client-side Javascript to fetch the "dynamic" content. Please note that I just threw this together, so it might be missing a parenthesis or two somewhere and you should add proper error handling and all that jazz.

Sign up to request clarification or add additional context in comments.

1 Comment

The trick with returning render_template as a html fragment helped me a lot! Thanks
1

Jinja2 itself is just a templating language. For re-populating a part of the HTML with data from your Python app I would use a JavaScript library like jQuery that can retrieve the data via AJAX and modify the DOM with the results. For example, your initial page would display "Updating…" in a div element. jQuery makes an AJAX call to a URL of your app that reads the directory and sends a HTML-formatted string (perhaps also rendered via Jinja) with the results which would replace the content of the "updating" div.

Comments

0

If you like the user to receive updates, you have to use the channel API. The channel API can send updates from a task to javascript in the client.

See this blogpost: http://blog.notdot.net/2011/07/Using-the-Channel-API-on-App-Engine-for-instant-traffic-analysis

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.