How to force auto reload of Flask in code? - python

I want to render a session of a bokeh plot on a flask server. The problem I have is, that the generated script tag of that session only produces the plot after I reload the flask server.
The reload takes place by saving the server file.
Is there a way to force this event in the underlying python code? Something like Flask.reload()?

My tip is: create a new route to process the information you want and make a request via javascript (jquery/ajax) to get the data and popularize the charts.
Javascript is responsible for performing this type of action. After a template is rendered, only JS can make changes in the DOM (no more python responsibility).

Related

Flask: Redirect after function/loading complete?

I am using Python 3.6.1 with Flask, with the goal being to load a webpage, display live loading information via websockets, then once the loading is complete redirect or otherwise allow the user to do something.
I have the first two steps working- I can load the page fine, and the websocket interface (running on a separate thread using SocketIO) updates properly with the needed data. But how can I make something happen once the functions that need to load are finished? To my understanding once I return a webpage in Flask it is simply static, and there's no easy way to change it.
Specific code examples aren't necessary, I'm just looking for ideas or resources. Thanks.

Pass js variable to Jinja template in Ckan

I want to pass javascript variable value to jinja2 template to query db according to some user requests.I can store user requests in js variable but I cannot pass variable value to Jinja.Do you have any idea or example?I use ckan project so I have to use Jinja template.I made some research maybe I have to use ajax request but how can I learn which function will be running in backend side as far as I know ckan use pyloans framework in backend side.How to render template in pyloans framework.Do you have any example?
As #E.Serra has already pointed out this is not possible: the Jinja-templates are executed on the server, while the JavaScript is executed afterwards in the browser.
Hence, you can either
compute the desired content when the page is first loaded and include it in your Jinja-template. For this, you would implement the ITemplateHelpers interface and implement a custom helper function which you can then call fro your template.
compute the desired content asynchronously after the page has loaded. For this you would send a web request from JavaScript to a CKAN API end point. If none of the existing API functions provide what you need then you can add your own using the IActions interface.

Best way to display python script data in a webpage?

I have a python script that collects data on some virtual machines. I want to display this data in a webpage.The web page must be dynamic since the script will run continuously and the data must be updated every time the script runs. I possibly want this data displayed in a table but I am not sure what direction to go?
You could use Apache or another web server. Set up an HTML web page with javascript. I recommend using jQuery as it makes making ajax calls so much easier. Have your javascript/ajax/jquery call your python script every x minutes/seconds.
Ensure your apache server is setup to run CGI scripts and ensure they are set with read and execute permissions.

Flask return nothing, instead of having the re-render template

I have written a python function using flask framework to process some data submitted via a web form. However I don't want to re-render the template, I really just want to process the data and the leave the web form, it the state it was in, when the POST request was created. Not sure how to do this ... any suggestions ?
If you want the user to stay in place, you should send the form using JavaScript asynchronously. That way, the browser won't try to fetch and render a new page.
You won't be able to get this behavior from the Flask end only. You can return effectively nothing but the browser will still try to get it and render that nothing for the client.

Update webpage based on sql database

As a non webdev, I'd really like if you could point me out to the 'correct' way to do this.
I built an application that populates and updates a database periodically (sqlite to be precise). I store leaderboards in my database, and would like to display them in a webpage.
As my leaderboards can change all the time, I need the webpage to be dynamic, either on a time based trigger or whenever the database changes.
I was about to use javascript for that, by realized that my database is hosted on my server, so client side js might not work.
Any ideas on that?
As my app is built in Python, I'd prefer avoid using php solutions but use more 'trendy' technologies (js, ruby, python, ? ? ? whatever)
Thanks!
Ok, given the keywords I got now here is an almost exact duplicate :
Notify user on database change? JavaScript/AJAX
This is what Ajax is for. You write Javascript on the front end that uses Ajax to call your server-side code to return the database content, then updates your HTML when it receives a response.
You need to use javascript on the client side not on the server. Your javascript code will make async calls (using ajax) to check if the db changed and update the website accordingly.
You can either use javascript to poll the server side periodically. Or you can use javascript to create (perhaps using a library like SockJS or SocketIO) a websocket connection that can actually push data to the client side when it changes. I do this on a number of projects using Tornado's websocket support on the server side.

Categories

Resources