Hello all.
User Story: React Native app, requests json from database (not essential), requests Python API to update database, and displays json how I wish.
Issues: I was not sure of the best method to implement this functionality. I already had a Python script that scraped data off the web, and I have it able to output this data to a .json
file. I want this controlled by a React app natively.
I have been trying many different scripts with Flask APIs to update the data, and allow requests. Here are some of my failed attempts:
SQLAlchemy
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user:password@host/database'
db = SQLAlchemy(app)
class Sermon(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80))
category = db.Column(db.String(120))
date = db.Column(db.String(120))
speaker = db.Column(db.String(120))
db.create_all()
db.session.add(Sermon(id=0,title='Test Title',category='Test Category',date='test Date',speaker='Test Speaker'))
db.session.commit()
@app.route('/test',methods=['GET'])
def testSQL():
Sermon.query.all()
mysql.connector
from flask import Flask, request, jsonify
import mysql.connector
# Create a new Flask application
app = Flask(__name__)
# Connect to database
sermonsDB = mysql.connector.connect(
host="host",
user="user",
passwd="password",
database="sermons"
)
cursor = sermonsDB.cursor()
# CREATE TABLE
cursor.execute("CREATE TABLE sermons (id INT PRIMARY KEY, title VARCHAR(255), date VARCHAR(255), speaker VARCHAR(255), category VARCHAR(255)")
# INSERT VALUES
sql = "INSERT INTO sermons (id, title, date, speaker, category) VALUES (%d, %s, %s, %s, %s)"
val = [
(0,"Test Title", "Test Date","Test Speaker","Test Category"),
(1,"Test Title 2", "Test Date 2","Test Speaker 2","Test Category 2")
]
cursor.executemany(sql, val)
sermonsDB.commit()
@app.route('/test',methods=['GET'])
def testSQL():
cursor.execute("SELECT * from sermons")
result = cursor.fetchall()
res = []
for x in result:
res.append(x)
# Run Debug
if __name__ == '__main__':
app.run(debug=True)
marshmallow
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from marshmallow_jsonapi.flask import Schema
from marshmallow_jsonapi import fields
from flask_rest_jsonapi import Api, ResourceDetail, ResourceList
# Create a new Flask application
app = Flask(__name__)
# Set up SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user:password@host/sermons'
db = SQLAlchemy(app)
# Define a class for the Artist table
class Sermon(db.Model):
id = db.Column(db.Integer, primary_key=True)
title= db.Column(db.String)
category = db.Column(db.String)
date = db.Column(db.String)
# Create the table
db.create_all()
# Create data abstraction layer
class SermonSchema(Schema):
class Meta:
type_ = 'sermon'
self_view = 'sermon_one'
self_view_kwargs = {'id': '<id>'}
self_view_many = 'sermon_many'
id = fields.Integer()
title= fields.Str()
category = fields.Str()
date = fields.Str()
class SermonMany(ResourceList):
schema = SermonSchema
data_layer = {'session': db.session,
'model': Sermon}
class SermonOne(ResourceDetail):
schema = SermonSchema
data_layer = {'session': db.session,
'model': Sermon}
api = Api(app)
api.route(ArtistMany, 'sermon_many', '/sermons')
api.route(ArtistOne, 'sermon_one', '/sermons/<int:id>')
# Run Debug
if __name__ == '__main__':
app.run(debug=True)
For all of these examples, I have been trying to host my API on https://www.pythonanywhere.com/
At one stage, using the marshmallow
package, I managed to request some data. I then changed some code, and since have not even been able to connect to the database anymore, and get told I have no permission.
There must be a better way of doing this. The marshmallow
package worked to an extent, but the data structures are far too complicated in their base configurations for the data I plan on working with.
Does anyone have any ideas for the following:
- To database or not to database?
- To Flask or not to Flask?
- To PythonAnywhere or not to PythonAnywhere?
- To API or not to API?
- To Python or not to Python? (By this, I mean: Should I scrap the Python webscraper, and find a way to implement the functionality using JavaScript, considering my app is written in JavaScript (I do not know how this will fair with being built and ejected for deployment))