last day (15 days later) » 

09:41
0
A: How to get client to read varaible from server with flask

turnip Disclaimer: You've stated that you have little experience with servers and clients so I will propose what is probably the easiest option to implement that would require the least technical knowledge / effort. This is certainly a non-ideal, untested, option and it doesn't tackle your qu...

That looks promising... I'm guessing that "function(response)" is javascript? and means do a call to a function called "function" with a parameter which could be a string containing the percentage completed? but I don't know how to make functions in javascript. Could I trouble you to add a function which simply displayed that number as text on the page... also, does there need to be a line that loads an axios library?
Hi @Mick. The function (response) bit is a nameless function. The response is an arugment - in this case it represents the response from the server. Here, the then() function requires us to give it another function to execute when the request succeeds. So we define a nameless function right in there that will print a line to the console and will stop the while loop. What you mentioned about the percentage is not feasible, no; that's not how it works unfortunately.
When you say "What you mentioned about the percentage is not feasible" - you mean that your code would only be suitable for displaying a spinning-wheel/egg-timer or similar and can't be used to actually indicate how much longer there is to go?
That is correct. You could do it, but it would not be great - for example, your my_job could also save in another file how much it has already processed. Then in the check_job endpoint you could read that file and return the percentage in the response. But, this would only be beneficial if the process you have is taking several minutes.At this stage you're adding so much complexity to your system for very little benefit.
Why would it "not be great"? You say "several minutes"... well, depending on the parameters the user selects the server could take anywhere from say 10 seconds to 2 minutes. In the two minute case the user could easily become concerned that the server had crashed and it would make for a very unpleasant user experience. If I could keep them updated on when the answer may arrive it would be so much better.
09:41
I meant that it's not a good way to solve the problem. What if 1000s of users use your application? Your server will run out of memory pretty quickly - to tackle that you will need to write something that safely cleans up these leftovers. In fact you'd have to do it anyway, because overtime these files will pile up. Moreover, the client will only be checking the server every X seconds right? That means the rate of updates is limited by X. Unless you choose to do this properly, I would suggest that you simply render a message on the client that says this may take up to 2-3 minutes.
@Mick, it might be easier to chat here if you wish
Hi. So the site is ver niche, there would never be 1000's of users. Actually I shouldn't really call it a site at all, I think it's more of a web app.
That's fine, I was just trying to demonstrate the point that it adds additional problems
Ok, looks good
If the user puts a big number in the "Num iterations to run" field then the simulation can take a long time.
09:48
Yep, just testing it out now
So here is what I would suggest
I really think that unless you opt to use websockets and learn a bit more about Javascript, that you just render a notice that warns the user that it might take a while
You could even do it conditionally, based on the number they input
Hmmm...
Here is another thing
The progress not only depends on the speed of the calculation, it also depends on the user's network speed
really? I don't think so...
Technically speaking, I could be using this site on a crappy network and it might take me 2 mins just to download the image
all the work is done on the server
09:51
Yes, but downloading the image takes time
In most cases, it won't matter
That's not a consideration - apart from anything they can see the process of the image downloading
Sure, but in the worst case they would see your progress bar say 100% and the image might take some time to arrive
It's a small point, I was just talking in principle
sure.
All in all, I don't think it's worth the added complexity on the server for you to do all this. The cheaper way to do this would be to run a few simulations yourself, check the time it takes, and create a fake progress bar on the frontend
Or, just display a simple warning
complexity on the server side does not worry me - I am comfortable with python... i can read and write files and delete old ones - my real issue is not knowing enough about client side stuff.
I don't need any great detail about what to do on the server side - just the gist will do fine, I can take it from there - my struggles are all on the on the client side
so you think I need to read up on websockets and javascript in order to solve this.
10:01
Yes. I can point you the right direction
You will need to add websocket support to your server. There is a great library called Flask-SocketIO for this.

On the client side you will need to use the socketIO library: https://socket.io/
OK, thanks for that. I will look them up.
I have two concerns however:
1) It looks like you were using server-side rendering. Not sure how easy it will be to setup the frontend with socketio in this way.
2) It looks like you're using a pre-packaged solution to host your actual server. So I don't know what server tech they're using - they might not support websockets - in the worst case it won't matter much - it just means that you will be stuck with long-polling
Hmmm... I just put "from flask_socketio import SocketIO" at the start of my python and the website broke :-(
maybe in need to pip install first ... on mo..
yes you'd have to install it first
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-
packages/python_engineio-3.12.1.dist-info'
I guess pythonanywhere.com don't allow it
10:13
I don't know how pythonanywhere works so I can't help you there unfortunately
How did you install Flask on there to begin with?
my hunch is that it's pre-installed... let me check
The link shows everything they've got pre-installed.
lots of things with the word sock in!
Are you stuck with Pythonanywhere or are you able to host it somewhere else?
Yeah, you could try to use a different socket library but I have no idea if it will work well with flask.
I don't absolutely have to put it there but it was a fair old challenge to A) find the free site that could handle it and B) get it all up and running.
Another options is AWS, have you used them before?
It's a fair bit more complicated
no. I have never made a web app before.
I've made a few pure-HTML static websites and that's about it
10:22
I would suggest you look into Elastic Beanstalk, a service that Amazon Web Services (AWS) provide. It comes with a free tier.

They have a Flask tutorial.
It's definitely a bit more complicated because it involves more to setup, but there are lots of resources online on how to do it
OK. looking now
Beanstalk doesn't support websockets, but I think in your case long-polling will do just fine.
Maybe if I use long polling I don't have to move?
Well, sure, but you'd have to choose a library that works from what they currently have installed
As I said, I have perosnally not used anything other that Flask-SocketIO for this kind of stuff so I won't be able to help
It would definitely be worth the effort to look into what libraries they offer first, before moving to AWS
Just found this page (blog.oddbit.com/post/2013-11-23-long-polling-with-ja) which uses "bottle" and "gevent" both of which are included in pythonanywhere.com
10:32
Keep in mind that Bottle is a replacement for Flask here
Oh!
Had no idea - that screws things up.. I will look elsewhere
You may not need FlaskSocketIO to be honest. Try googling for "long polling Flask"
Gevent works with flask, so you might just need that
I'm just unsure on the implementation details regarding how you'd emit events to the client
is uses flask-restful which is included
Give it a go!
OK, will do... actually I need to attend to a couple of other things right now - will take this up a bit later today.
Thank you very much for all your help
10:41
No problem. I hope you get it working.

last day (15 days later) »