1

I am working on making a GUI front end for a Python program using HTML and CSS (sort of similar to how a router is configured using a web browser). The program assigns values given by the user to variables and performs calculations with those variables, outputting the results. I have a few snags to work out:

  1. How do I design the application so that the data is constantly updated, in real-time? I.e. the user does not need to hit a "calculate" button nor refresh the page to get the results. If the user changes the value in a field, all other are fields simultaneously updated, et cetera.

  2. How can a value for a variable be fetched from an arbitrary location on the internet, and also constantly updated if/when it is updated at the source? A few of the variables in the program are based on current market conditions (e.g. the current price of gold). I would like to make those values be automatically entered after retrieving them from certain sources that, let us assume, do not have APIs.

  3. How do I build the application to display via HTML and CSS? Considering Python cannot be implemented like PHP, I am seeking a way to "bridge the gap" between HTML and Python, without such a "heavy" framework like Django, so that I can run Python code on the server-side for the webpage.

I have been looking into this for a quite some time now, and have found a wide range of what seem to be solutions, or that are nearly solutions but not quite. I am having trouble picking what I need specifically for my application. These are my best findings:

Werkzeug - I believe Werkzeug is the best lead I have found for putting the application together with HTML and Python. I would just like to be reassured that I am understanding what it is correctly and that is, in fact, a solution for what I am trying to do.

WebSockets - For displaying the live data from an arbitrary website, I believe I could use this protocol. But I am not sure how I would implement this in practice. I.e. I do not understand how to target the value, and then continuously send it to my application. I believe this to be called scraping?

2 Answers 2

1

Question 1. For that you would need javascript, javascript is used for adding behaviour to websites. I would recommend using a javascript framework called Jquery for that.

You would do this by adding an html "id" or "class" to the inputs that have the value you want to recieve from the users and an "id" to the field where you would display the results of those calculations.

You would create an javascript event that basically tracks whenever any html object that has the class you used for the inputs above was changed, when that happens you do your calculations, and post the results on the result field you decided, using it's "id" identifier. Take a look at this link for more info

Question 2. For that you would have to do what is called "Web scraping": First you read the html source code of the page you want to get the information from and look at the ID of the HTML object you want to get the information from (If it doesn't have an ID, you would then have to look at the Hierarchy of it (What is also known as the Document Object Model DOM), looking at with Objects it's inside of) then you create a script that reads the HTML and looks for that ID or follows the Hierarchy you discovered and reads the text in that HTML object. Remember that HTML is just text.

I'm more used to the Ruby programming language, in ruby there is a module you download for that, but I'm sure that there is one called "Mechanize" for Python.

Question 3. You could use Flask which is a light and simple micro-framework for python Python, from the needs you describe, it sounds like perfect fit.

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

2 Comments

Excellent. Thank you very much, and I certainly feel you have put me down the correct path. Upon research your point on jQuery I stumbled upon the oninput event attribute in HTML5 (demonstrated here: w3schools.com/tags/tryit.asp?filename=tryhtml5_ev_onchange). I feel this may be a better solution considering it would not require using any framework.
Yeah, a framework is not required specially if what you are doing is small scale, it's really a matter of personal preference.
0

To achieve your goal, you need good knowledge of javascript, the language for dynamic web pages. You should be familiar with dynamic web techniques, AJAX, DOM, JSON. So the main part is on the browser side. Practically any python web server fits. To "bridge the gap" the keyword is templates. There are quite a few for python, so you can choose, which one suites you best. Some frameworks like django bring their own templating engine. And for your second question: when a web site doesn't offer an API, perhaps the owner of the site does not want, that his data is abused by others.

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.