Forums

I can't get my API to run! Help me!

I'm hosting my own API made with Python and Flask, on the PythonAnywhere platform. My database to be queried is a "csv file", I've placed it in the "mysite/" folder along with the API code "flask_app.py". But it can't query the database, do I have to change directory, or is it something in the wsgi address?

/var/www/maprado_pythonanywhere_com_wsgi.py

Hi, I've replied to the email you sent to support@pythonanywhere so let's keep the conversation going there

Hi Sam, this is source code at API:

import pandas as pd

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')

def homepage():

    return 'A API está no ar'

@app.route('/pegar_vendas')

def pegar_vendas():

    tabela = pd.read_csv('Criando API no Python.csv')

    total_vendas = tabela['Vendas'].sum()

    resposta = {'total_vendas': total_vendas}

    return jsonify(resposta)


if __name__ == "__main__":

    app.run(debug=True)

The code that queries the file it's:

import requests

import json

link = 'https://maprado.pythonanywhere.com/'

requisicao = requests.get(link)

print(requisicao)

print(requisicao.json())

dicionario = requisicao.json()

print(dicionario['total_vendas'])

That's it,

Thank you

Sam.

Use absolute path to the file not the relative one.

I tried using the absolute path, but it still gave an error.

https://www.pythonanywhere.com/user/MaPrado/files/home/MaPrado/mysite/

In fact, the error says that I'm receiving HTML from the API and not JSON, it's HTML from Pythonanywhere, an error alert screen: "Something went wrong :-(".

Which path exactly are you adding to sys.path in the wsgi file? If your flask_app.py is in /home/MaPrado/mysite you should add /home/MaPrado/mysite to the path and reload your web app.