I've been trying to make a website that gets information from another website (top 10 best tetris players) and then log it into my console. I'm experimenting with pandas for Python and Javascript with ajax.
The original Python code worked in nodejs, but I need it to work on a website, so I tried to adapt by using ajax.
Now I'm getting this error code1 and sometimes this error code2. I'm not sure when which one happens. Always code 404 though.
Does anyone know how to fix this? Thanks in advance!
HTML/Javascript code:
<html>
<head>
<title>website test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<button>click me for nothing to happen</button>
<script>
function ajaxCall() {
$.ajax({
url: "/makeTop10",
success: function (data) {
console.log(data);
console.log("call happened.") //extra test
}
});
}
ajaxCall();
</script>
</body>
</html>
Python code:
from flask import Flask, render_template
import pandas as pd
app = Flask(__name__)
@app.route("/makeTop10")
def makeTop10():
list = pd.read_html("https://listfist.com/list-of-tetris-high-scores-nes-pal")
data = list[0]
output = ""
for i in range(10):
output += "{: <10}{: <20}{: <25}\n".format(
data.at[i, "Rank"],
data.at[i, "Player Name"],
data.at[i, "Score(2 April, 2022)[source]"],
)
return output
if __name__ == "__main__":
app.run()
I just changed your code to this and it's working
HTML/Javascript code:
<html>
<head>
<title>website test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<button>click me for nothing to happen</button>
<script>
function ajaxCall() {
$.ajax({
url: "http://127.0.0.1:5000/makeTop10",
success: function (data) {
console.log(data);
console.log("call happened.") //extra test
}
});
}
ajaxCall();
</script>
</body>
</html>
Python code:
from flask import Flask, render_template
import pandas as pd
from flask_cors import CORS # added
app = Flask(__name__)
CORS(app) # added
@app.route("/makeTop10")
def makeTop10():
list = pd.read_html("https://listfist.com/list-of-tetris-high-scores-nes-pal")
data = list[0]
output = ""
for i in range(10):
output += "{: <10}{: <20}{: <25}\n".format(
data.at[i, "Rank"],
data.at[i, "Player Name"],
data.at[i, "Score(2 April, 2022)[source]"],
)
return output
if __name__ == "__main__":
app.run()