Anpassat till den nya designen där Flask skapar websidor m.h.a. mallar. Webfiler (html, css, js) har flyttats till egna kataloger.

This commit is contained in:
2024-07-25 00:41:50 +02:00
parent f179e8e19b
commit ccc8e73f48
5 changed files with 42 additions and 11 deletions
+21 -4
View File
@@ -1,25 +1,42 @@
# Import the necessary functions from ollama, Flask, requests, threading
from ollama import Client
from flask import Flask, request, jsonify, send_from_directory
from flask import Flask, request, jsonify, send_from_directory, render_template
from flask_cors import CORS, cross_origin # CORS stands for Cross-Origin Resource Sharing. This is necessary to allow the frontend to make requests to our backend.
import requests
import json
import logging
import os
# from utils import set_local_logger
logger = logging.getLogger(__name__) # Separate logger for this module
# set_local_logger(logger) # Set log level for logger
# Create a logger for this module
logger = logging.getLogger(__name__) # This logger will be used to log messages from this module
logger.debug("Logging level of backend logger has been configured")
# Find out the path to current directory according to the Python interpreter (venv)
logger.debug("Current working directory: %s", os.getcwd())
# Initialize a Flask application
app = Flask(__name__)
app.config['STATIC_FOLDER'] = 'static' # Adjust if needed
logger.debug("flask app template folder: %s", app.template_folder)
@app.route('/')
def index():
"""
This route serves index.html to connecting clients
"""
logger.debug("Entering route '/'")
api_endpoint = os.environ['BE_API_ENDPOINT'] # Retrieve the environment variable
return render_template('index.html', api_endpoint=api_endpoint)
logger.debug("API endpoint: %s", api_endpoint)
with open('smartassist/src/html/client.html', 'r') as f:
client_html = f.read()
# logger.debug("Client HTML (first few characters): %s", client_html[:50]) # Print to see if it's loading
logger.debug("Client HTML (first few characters): %s", client_html) # Print to see if it's loading
return render_template('index.html', api_endpoint=api_endpoint, client_content=client_html)
@app.route('/<path:filename>')
def serve_static(filename):
+3 -2
View File
@@ -3,7 +3,8 @@
<head>
<meta charset="UTF-8">
<title>Ollama Chat</title>
<link rel="stylesheet" href="../css/clientstyle.css">
<link rel="stylesheet" href="/css/clientstyle.css">
<!-- <link rel="stylesheet" href="python_test/smartassist/src/css/clientstyle.css"> -->
</head>
<body>
<h1>Ollama Chat</h1>
@@ -13,7 +14,7 @@
<textarea id="userInput" placeholder="Type your message..." rows="5"></textarea>
<button onclick="sendMessage()">Send</button>
<script src="../js/frontend.js"></script>
<script src="/js/frontend.js"></script>
<script>
const chatContainer = document.getElementById('chatbox');
+16 -3
View File
@@ -1,15 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<title>Frontend</title>
<!-- <title>Frontend</title> -->
</head>
<body>
<!-- This iframe will hold the content from client.html -->
<iframe id="client-frame" style="width: 100%; height: 100vh;" srcdoc="{{ client_content }}"></iframe>
<!-- Responsive scaling and some padding -->
<script>
const clientFrame = document.getElementById('client-frame');
function resizeIframe() {
clientFrame.style.height = window.innerHeight - 50 + 'px'; // Adjust the subtraction for padding/margins if needed
}
window.addEventListener('resize', resizeIframe);
resizeIframe(); // Call it once on page load
</script>
<script>
const apiEndpoint = '<%= api_endpoint %>'; // Templating syntax (Jinja2)
console.log("API Endpoint:", apiEndpoint);
// Use apiEndpoint in your frontend code...
</script>
</body>
</html>