Skip to content
Open
85 changes: 41 additions & 44 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
import json
import os
from flask import Flask, render_template, request
from flask_cors import CORS
from helpers.MySQLDatabaseHandler import MySQLDatabaseHandler
import pandas as pd

# ROOT_PATH for linking with all your files.
# Feel free to use a config.py or settings.py with a global export variable
os.environ['ROOT_PATH'] = os.path.abspath(os.path.join("..",os.curdir))

# Get the directory of the current script
current_directory = os.path.dirname(os.path.abspath(__file__))

# Specify the path to the JSON file relative to the current script
json_file_path = os.path.join(current_directory, 'init.json')

# Assuming your JSON data is stored in a file named 'init.json'
with open(json_file_path, 'r') as file:
data = json.load(file)
episodes_df = pd.DataFrame(data['episodes'])
reviews_df = pd.DataFrame(data['reviews'])
import subprocess
import threading
import time
from flask import Flask, render_template
import requests

app = Flask(__name__)
CORS(app)

# Sample search using json with pandas
def json_search(query):
matches = []
merged_df = pd.merge(episodes_df, reviews_df, left_on='id', right_on='id', how='inner')
matches = merged_df[merged_df['title'].str.lower().str.contains(query.lower())]
matches_filtered = matches[['title', 'descr', 'imdb_rating']]
matches_filtered_json = matches_filtered.to_json(orient='records')
return matches_filtered_json

@app.route("/")
def home():
return render_template('base.html',title="sample html")

@app.route("/episodes")
def episodes_search():
text = request.args.get("title")
return json_search(text)

if 'DB_NAME' not in os.environ:
app.run(debug=True,host="0.0.0.0",port=5000)

# Function to check if Streamlit is running
def check_streamlit():
try:
response = requests.get("http://10.48.49.54:8501")
return response.status_code == 200
except requests.ConnectionError:
return False

# Function to run Streamlit in a separate thread
def run_streamlit():
subprocess.run(["streamlit", "run", "streamlit_app.py"])

# Start Streamlit in the background and ensure it starts before rendering Flask page
@app.before_request
def before_first_request():
# Start Streamlit in a separate thread
threading.Thread(target=run_streamlit, daemon=True).start()

# Wait for Streamlit to be available
while not check_streamlit():
time.sleep(1) # Check every second

@app.route('/')
def index():
# Ensure Streamlit is available before loading the page
return render_template('streamlit_embed.html')
#

if __name__ == '__main__':
app.run(debug=True, use_reloader=False) # `use_reloader=False` to avoid restarting Flask server when Streamlit starts


## currently the html file is loading form the localhost port 8501,
# and then you should change it to the server backend.
# the server backend can be found in the server site and not the local host.
Loading