#๐ Load local JSON file within Javscript run on Flask App
29 messages ยท Page 1 of 1 (latest)
@summer flame
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
?
apparently i cant upload html files
Well, then at least try to explain your scenario first
i did, it deleted it
Why? Because nothing is clear on what you are trying to do
I typed a whole paragraph, and the bot deleted it.
Ok, then try again because I don't know how to help you then
Hello! I am trying to write a dropdown in javascript popluated by a json, and have found that using python to read the local json file would be easier. How would I call this python script from javascript? keep in mind this js is within a html file, which is the template for my Flask webapp. How could I call this python script?
Click here to see this code in our pastebin.
<head>
<title>Log Scrubber Webapp</title>
</head>
<body>
<div>Input Names</div>
<!-- <label for="name"></label> <input type="text" id="name" name= "name"> -->
<form>
<input list="names" id="name">
<datalist id="names"></datalist>
<button type="submit" onclick="getNames()">Enter</button>
</form>
</body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(function() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "jsonconvert.py", true);
xhr.onload = function(e) {
alert(xhr.response)
}
xhr.send();
// $.getJSON("users.json", function(data){
// console.log(data.users);
// }).fail(function(){
// console.log("An error has occurred.");
// });
var users = [];
var list = document.getElementById('names');
users.forEach(function(item){
var option = document.createElement('option');
option.value = item;
list.appendChild(option);
});
});
function getNames() {
var username = '';
username = document.getElementById("name").value;
const commands = ["/msg ", "/w ", "/tell "];
const searchTerms = [];
const filePath = "./files/";
const usernames = [username];
const dict_values = {usernames, commands, searchTerms, filePath};
const s = JSON.stringify(dict_values);
console.log(s)
$.ajax({
url:"/test",
type:"POST",
contentType: "application/json",
data: JSON.stringify(s)})
}
</script>
</html>
and here is my current html, which gets a 404 error when getting the python file
May I know why you are trying to get python file? I mean what is the use case you are trying do here? Do you want to pass JSON data from client i.e. frontend to backend?
you would have to post the json to an url that is handled by your flask application if it is the client that has access to the json data that you want python to process
essentially, this is all local- this app is intended to sit on the serverbox and allow somone inexperienced with code to use the system. The web app is locally hosted, with a local json file in the same folder.
how would i post this json to a aurl handled by flask?
whether its the python file or straight to the json, I want to access the json file within the frontend. I dont need it to go to the backend
And where is this json file stored? Must be on the server, right?
currently, as im developing this, the file is just stored in the directory
No, finally where it will be? Must be on the server, right? Because then you will have to call the server to get the json data back
Try this approach: https://stackoverflow.com/questions/7346563/loading-local-json-file#answer-7346598
I think at deployment, the json file would be hosted on a server, so I could then point to it properly. However, I am not developing directly on the serverbox, so where I am running it isnt equivalent. Now im trying to find a way to host the json as a server
Well, if that's the case then your approach is wrong. Try to call server to get the data from the json file, do not try to read locally because it will rework for you again.
if i knew the file path from the server i could do that, for sure- i dont know it atm, but when i do ill give it a try, ty
Well, then where you are running the flask server?
locally
I used tiinyhost to host my json file and now i can access it!
Well, yeah then you can just call the api in backend, get the json data back from json file, it's simple
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.