whatevermike
whatevermike

Reputation: 196

Parse HTML using beautiful soup & render template in jinja2

I'm trying to parse a local HTML document using beautiful soup, then render_template() the result using jinja2.

I'm new to python, but here's what I'm trying:

@app.route("inherit/index")
def inheritIndex():
    soup = BeautifulSoup(open("templates/index.html"), "html.parser")
    soup.find(text="foobar").replaceWith("Hooray!")
    return render_template(soup)

Upvotes: 0

Views: 1807

Answers (1)

whatevermike
whatevermike

Reputation: 196

I managed to substitute the values right from within the render_template() method. BeautifulSoup was not required. Here was my solution as suggested in the comments.

HTML:

...
<p> {{ foobar }} lorem ipsum dolor...</p>
... 

Python:

@app.route("inherit/index")
    def inheritIndex():
    return render_template("index.html", foobar="Hooray!")

    # <p> Hooray! lorem ipsum dolor...</p>

Upvotes: 1

Related Questions