I'm developing a web app where I need to update the configuration of a certain type of graph. To do so, I'm using Python3 with flask and html.
I want to update the configuration dynamically, which means that I don't want to use the function render_template and instead, I want to use return jsonify(message) .
Below I included the function that I want to use to update the data. This function is used as a callback on the rest of my system and those prints are being executed well, which means that on the flask side, everything seems to be okay. However, I don't know what I should put on the html side to receive this data.
Here is the code that I'm using in flask to send the data:
#app.route('/online_visualization_update')
def online_visualization_update(msg):
print("I should change the network.")
print("The message was ", msg)
with app.app_context():
return jsonify(msg.transition, msg.marking)
This is the code I tried to implement on html to receive the data sent by flask:
<script type=text/javascript>
fetch('/online_visualization_update')
.then(response => response.json())
.then(data => console.log(data))
.catch((error) => {console.error('Error:', error);
});
</script>
Currently, this is not working because the the fetch is being performed every time I open the webapp. What am I doing wrong?
Ideally, I would like to have a function on javascript that is executed only when it receives the data from flask but I don't know whether fetch works like that or not.
Thank you
For that you need to use Fetch API in javascript like this
// write function like this
function callApi(){
fetch('/online_visualization_update')
.then(response => response.json())
.then(data => console.log(data))
.catch((error) => {
console.error('Error:', error);
});
}
Refer this for more about fetch:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
Add this code in your Html page:
<Div class="container">
<button type="button" onclick="callApi()">Call func</button>
<!--you need to call function like this whenever you need data -->
</Div>
Related
As a learner, I am trying to call the rest api. For example, you can see and copy the url from api provider as they listed on that link page.
https://docs.api.jikan.moe/#tag/top/operation/getTopAnime
function App() {
const [topAnime, SetTopAnime] = useState([]);
useEffect(() => {
fetch(`https://api.jikan.moe/v4/top/anime`)
.then((response) => response.json())
.then((data) => {
console.log(data);
SetTopAnime(data);
})
.catch((err) => {
console.log(err.message);
});
}, []);
But the question is; this does not let me to call a specific data I want to call.
To do that, I need to add some query parameters as api page written for developer.
Now, I see several examples that someone set it as follows:
const getData = () => {
axios
**.get(`${apiTop}?sfw=true&limit=20`)**
.then((res) => {
return setData(res.data.data);
})
.catch((error) => {
console.log(error);
});
};
that .get method and following code makes sense. But how that developer who coded that line knew '?sfw=true&' such thing?
If I don't understand this; what topic could be the one I should review then? I believe I understand what promises and fetch are, but not sure focusing on 'query parameter' is the right thing to solve my problem.
tl;dr
To call a specific data from getTopAnime, how should I set query parameters? (with my understanding, https://api.jikan.moe/v4/top/anime/type/movie
or
https://api.jikan.moe/v4/top/anime?=query/type/movie
is the limit to expand the thoughts.
Thanks...
From the documentation of the API -- https://docs.api.jikan.moe/
There must be API documentation where you can read about these URI structures.
Though a real REST service sends you hyperlinks with URIs or URI templates which you follow, so you need to know what type of hyperlink watch for and the service gives you the URI structure. This is called HATEOAS constraint.
I have a page that has much data I want to use. I inspected the page with Google Chrome to find the URL of the data (Network XHR). I get the request URL and when I open it, it does not show me anything, but in the preview and in the responses I can see the data. I tested some JavaScript codes to get JSON data from this URL, but it always errors.
I tested some command in console to display the text responses in console but nothing please I need your help to get this data using JavaScript
some codes I tested:
fetch( 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?&action=get_vehicles' )
.then( response => response.text() )
.then( response => {
.then(function(data) {
console.log(data.data);
} );
`
headers
headers
responses
preview
You can get the fetch api call by going in the network tab in Chrome Developer Tools, clicking with right button in the request and then clicking in copy > copy as fetch like so:
fetch("https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?&action=get_vehicles")
.then((response) => response.json())
.then((data) => console.log(data))
.catch((err) => console.log(err));
how to parse json from an url into html by using javascript (programmatically) and i facing an problem with this ?
api: https://corona.ps/API/summary
You need to use the built in JavaScript fetch() method which returns a promise. Use .then() to handle the response and .catch() to handle any errors relating to the fetch call.
Here's an example function using your api link:
function getData () {
fetch("https://corona.ps/API/summary")
.then(res => res.json())
.then(data => console.log(data)) // will log js object
.catch(err => console.log(err))
}
The Fetch API will not run when I try to get a base64 string. What am I missing?
I've tried using a standard fetch in javascript, which is fine on all other calls until it's base64. It doesn't even reach the alert.
fetch(vimgurlone)
.then(response => response.json())
.then(dataimage1 => {
alert("here");
document.getElementById("theactualdata").innerHTML = dataimage1.Content;
})
.catch(error => console.error(error))
The json response will be something like:
{ Content: "brtergbrtbrtbwrtnhtehrth4t5h......" }
I would like receive and then assign the data to a variable and then display it or at least get to the alert message in the code above. The variable/URL "vimgurlone" is valid and displays a json response when pasted into a browser, but the fetch will not run. Do I need to decode it or something? The base64 string can be quite long. Thanks.
Ignore me! the issue was the API hadn't enabled cross origin requests, the code above is fine.
Thanks for the suggestions.
I am trying to use the d3.json method to access JSON Data from an endpoint. I am able to successfully access the data using the fetch method below, however I am not sure how to change this to the d3.json method. How can I pass the argument "credentials:include"
fetch('http://0.0.0.0:example.com',{
credentials: 'include'
})
.then(response => response.json())
.then( json => console.log(json))
My attempt would be something like this, however I am not sure where I would add the credentials argument.
d3.json('http://0.0.0.0:example.com', json => console.log(json))
d3.json use fetch so the same will work
From the doc:
d3.json:
Fetches the JSON file at the specified input URL. If init is specified, it is passed along to the underlying call to fetch; see RequestInit for allowed fields.
d3.json('http://0.0.0.0:example.com', {credentials: "include"}, function(error, json) {
console.log(json);
})