Olwar
Olwar

Reputation: 1

Fastapi works with uvicorn but not when deployed

I've deployed an api with Fastapi that worked perfectly when using uvicorn to host it locally. Now when I deployed it with Deta, it shows "500 internal server error".

Is there something wrong with my main.py? In the Deta Visor it only says response is "Internal server error" and also "No errors".

My main.py:

#!/usr/bin/python3
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import sqlite3

from fastapi.params import Depends

app = FastAPI()

origins = [
    "*"
]

async def get_db():
    conn = sqlite3.connect("C:/Users/Administrator/Desktop/CryptoBotDB.sqlite", check_same_thread=False)
    try:
        yield conn
    finally:
        conn.close()

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
async def root(db = Depends(get_db)):
    cur = db.cursor()
    
    cur.execute("SELECT money_start_total FROM Trades")
    trades = cur.fetchall()
    profit_lista = []
    for item in trades:
        profit_lista.append(round(int(item[0]), 0))

    profit_perc_lista = []
    cur.execute("SELECT profit_perc FROM Trades")
    profit_perc = cur.fetchall()
    for item in profit_perc:
        profit_perc_lista.append(str(round(int(item[0]), 0)) + "%")

    cur.execute("SELECT month FROM Trades")
    months = cur.fetchall()
    month_lista = []
    for item in months:
        month_lista.append(item[0][:2] + '/20' + item[0][2:])
        
    profit_dict = {"data": [{'data': profit_lista, 'labels': month_lista}]}
    data_lista = []

    for i in range((len(trades))):
        data_lista.append({"month": month_lista[i], "money": profit_perc_lista[i], "style": "color:red"})
    data_lista.reverse()

    cur.execute("SELECT money_end_total from Trades")
    money_now = cur.fetchall()[-1][-1]
    if money_now > 3001.0:
        total_profits = '+' + str(round((1 - (int(money_now) / 3001.0)) * 100, 2)) + '%'
    else:
        total_profits = '-' + str(round((1 - (int(money_now) / 3001.0)) * 100, 2)) + '%'
    total_profits_cash = str(round(money_now - 3001.0, 2)) + '€'

    total_profits_dict = {"profits_perc": total_profits, "profits_cash": total_profits_cash}

    cur.execute("SELECT profperc from cur_pos")
    cur_perc = cur.fetchone()
    cur.execute("SELECT profcash from cur_pos")
    cur_cash = cur.fetchone()

    cur_dictionary = {"cur_perc": cur_perc, "cur_cash": cur_cash}

    return profit_dict, data_lista, total_profits_dict, cur_dictionary

Upvotes: 0

Views: 1034

Answers (1)

Mustafa Abdelhai
Mustafa Abdelhai

Reputation: 40

It seems like you are trying to read a SQLite database that doesn't exist inside your Deta Micro. You are referencing a "C" Drive, which is only accessible to your code locally.

To solve this, make sure to include the database file with you code, next to the "main.py" file and deploy. Then you need to adjust your db code like this:

async def get_db():
    conn = sqlite3.connect("CryptoBotDB.sqlite", check_same_thread=False)
    try:
        yield conn
    finally:
        conn.close()

If you ever get stuck, feel free to reach out to us (the Deta team) on Discord/Slack/Intercom (links on our website) – we're pretty responsive.

Upvotes: 1

Related Questions