1

Most of the pertinent questions I see answered online revolve around accessing JS variables from parsed pages. I want to go the other way as I have an HTML page which is being printed in python by beautifulsoup. Pretty easy stuff except that the page contains a bunch of dynamic JavaScript (GUI stuff) which I am not sure how to pre-populate on render without things getting messy.

To clarify: The page is being rendered by a python script running on a server. The python script retrieves a bunch of market data which it slices/dices and then is supposed to populate various JS variables in my HTML page. Again the static HTML is fairly straightforward and I've done that before. But populating the JS variables and arrays could get tricky. FYI - this is a single page and thus it's not worth setting up Flask or Django.

Finally, I wonder if it may just be easier to skip beautifulsoup and simply parse a static HTML file and pre-populate placeholder strings.

Thanks for any pointers, insights, or even better: examples ;-)

0

1 Answer 1

1

If I understood it correctly you could try to create an script tag at the end of the html file with the help of beautifulsoup. In this script tag you could just set the variables like this.

your_soup is the soup element of your page

from bs4 import BeautifulSoup

variable = "Example"
temp_soup = BeautifulSoup('<script>var yourvariable = ' + variable + '</script>')
script_tag = temp_soup.html.body.script
your_soup.body.insert(len(your_soup.body.contents), script_tag)

I hope it works.

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

1 Comment

Yeah maybe I'm overthinking this. I also wonder if it may be just easier to read in the entire HTML file (incl. script, CSS tags, etc.) and just replace a bunch of placeholder strings. I'm sure there's a python lib for that out there.

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.