I was trying to make an api using python that I could just link with my react native project.
from flask import Flask
app = Flask(__name__)
import scratchconnect
from flask import request
# user = scratchconnect.ScratchConnect("SaiSantoshPal", "sai2010**")
# project = user.connect_project(project_id=733246147)
# variables = project.connect_cloud_variables()
# variables.get_variable_data(limit=100, offset=0) # Returns the cloud variable data
# balance = variables.get_cloud_variable_value(variable_name="balance", limit=100)
# balances = balance[0]
@app.route('/balance/')
def balance():
user = scratchconnect.ScratchConnect("SaiSantoshPal", "sai2010**")
project = user.connect_project(project_id=733246147)
variables = project.connect_cloud_variables()
variables.get_variable_data(limit=100, offset=0) # Returns the cloud variable data
balance = variables.get_cloud_variable_value(variable_name="balance", limit=100)
balances = balance[0]
return balances
@app.route(f'/add_balance/<int:amount>/')
def add_balance(amount):
# user = scratchconnect.ScratchConnect("SaiSantoshPal", "sai2010**")
# project = user.connect_project(project_id=733246147)
# variables = project.connect_cloud_variables()
# variables.get_variable_data(limit=100, offset=0) # Returns the cloud variable data
# balance = variables.get_cloud_variable_value(variable_name="balance", limit=100)
# balances = balance[0]
user = scratchconnect.ScratchConnect("SaiSantoshPal", "sai2010**")
project = user.connect_project(project_id=733246147)
variables = project.connect_cloud_variables()
variables.get_variable_data(limit=100, offset=0)
balancese = variables.get_cloud_variable_value(variable_name="balance", limit=100)
set = variables.set_cloud_variable(variable_name="balance", value=amount + int(balancese[0]))
balancese = variables.get_cloud_variable_value(variable_name="balance", limit=100)
# write_history(typeOf="add", add=amount)
return balancese[0]
@app.route(f'/sub_balance/<int:amount>/')
def sub_balance(amount):
# user = scratchconnect.ScratchConnect("SaiSantoshPal", "sai2010**")
# project = user.connect_project(project_id=733246147)
# variables = project.connect_cloud_variables()
# variables.get_variable_data(limit=100, offset=0) # Returns the cloud variable data
# balance = variables.get_cloud_variable_value(variable_name="balance", limit=100)
# balances = balance[0]
user = scratchconnect.ScratchConnect("SaiSantoshPal", "sai2010**")
project = user.connect_project(project_id=733246147)
variables = project.connect_cloud_variables()
variables.get_variable_data(limit=100, offset=0)
balancese = variables.get_cloud_variable_value(variable_name="balance", limit=100)
set = variables.set_cloud_variable(variable_name="balance", value=int(balancese[0])-amount)
balancese = variables.get_cloud_variable_value(variable_name="balance", limit=100)
# write_history(typeOf="sub", sub=amount)
return balancese[0]
@app.route(f'/sub_balance_petrol/<int:amount>/')
def sub_balance_petrol(amount):
# user = scratchconnect.ScratchConnect("SaiSantoshPal", "sai2010**")
# project = user.connect_project(project_id=733246147)
# variables = project.connect_cloud_variables()
# variables.get_variable_data(limit=100, offset=0) # Returns the cloud variable data
# balance = variables.get_cloud_variable_value(variable_name="balance", limit=100)
# balances = balance[0]
try:
user = scratchconnect.ScratchConnect("SaiSantoshPal", "sai2010**")
project = user.connect_project(project_id=733246147)
variables = project.connect_cloud_variables()
variables.get_variable_data(limit=100, offset=0)
balancese = variables.get_cloud_variable_value(variable_name="balance", limit=100)
set = variables.set_cloud_variable(variable_name="balance", value=int(balancese[0])-amount)
balancese = variables.get_cloud_variable_value(variable_name="balance", limit=100)
# write_history(typeOf="subp", sub=amount)
return f"Successful paid {amount}P!"
except Exception:
return "Transaction failed"
app.run(debug=False, port=2000)
My WSGI file:
# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project
import sys
# add your project directory to the sys.path
project_home = '/home/Sai0Santosh0Pal/mysite'
if project_home not in sys.path:
sys.path = [project_home] + sys.path
# import flask app but need to call it "application" for WSGI to work
from flask_app import app as application # noqa
My Error Log:
2022-09-24 12:05:18,203: ***************************************************
2022-09-24 12:05:29,974: Error running WSGI application
2022-09-24 12:05:29,975: ModuleNotFoundError: No module named 'scratchconnect'
2022-09-24 12:05:29,975: File "/var/www/sai0santosh0pal_pythonanywhere_com_wsgi.py", line 16, in <module>
2022-09-24 12:05:29,975: from flask_app import app as application # noqa
2022-09-24 12:05:29,976:
2022-09-24 12:05:29,976: File "/home/Sai0Santosh0Pal/mysite/flask_app.py", line 3, in <module>
2022-09-24 12:05:29,976: import scratchconnect
2022-09-24 12:05:29,976: ***************************************************
2022-09-24 12:05:29,976: If you're seeing an import error and don't know why,
2022-09-24 12:05:29,976: we have a dedicated help page to help you debug:
2022-09-24 12:05:29,976: https://help.pythonanywhere.com/pages/DebuggingImportError/
2022-09-24 12:05:29,976: ***************************************************