Przeglądaj źródła

Add load function

Also display flash messages if needed
master
The Dod 1 rok temu
rodzic
commit
7ccde933ec
5 zmienionych plików z 83 dodań i 6 usunięć
  1. 25
    3
      app.py
  2. 6
    0
      static/css/style.css
  3. 14
    2
      templates/chat.html
  4. 37
    0
      templates/load.html
  5. 1
    1
      templates/save.html

+ 25
- 3
app.py Wyświetl plik

@@ -1,5 +1,5 @@
1 1
 from flask import ( Flask, render_template, request, abort, redirect,
2
-    send_from_directory, url_for, session)
2
+    send_from_directory, url_for, session, flash, get_flashed_messages)
3 3
 from flask_session import Session
4 4
 import openai
5 5
 from dotenv import load_dotenv
@@ -203,14 +203,22 @@ def choices(topic):
203 203
 @application.route("/save", methods=['GET', 'POST'])
204 204
 def save():
205 205
     if request.method=="POST":
206
-        filename=request.form["filename"].rsplit("/",1)[-1]
206
+        filename = request.form["filename"].rsplit("/",1)[-1]
207 207
         if filename:
208
+            path = "archive/{}.json"
209
+            is_overwrite = os.path.isfile(path)
208 210
             moment = {
209 211
                 key: session.get(key, [])
210 212
                 for key in ["messages", "history"]
211 213
             }
212 214
             print(moment)
213
-            json.dump(moment, open("archive/{}.json".format(filename),"w"), indent=4)
215
+            json.dump(moment, open(path,"w"), indent=4)
216
+            if is_overwrite:
217
+                flash("Successfully saved to {}.json".format(filename))
218
+            else:
219
+                flash("Successfully overwritten {}.json".format(filename))
220
+        else:
221
+            flash("Invalid filename. Save aborted.", "error")
214 222
         return redirect(url_for("home"))
215 223
     else:
216 224
         return render_template("save.html",
@@ -218,6 +226,20 @@ def save():
218 226
                   "moment-%Y-%m-%d-%H.%M.%S"),
219 227
                 files = [os.path.basename(path).rsplit(".",1)[0] for path in glob("archive/*.json")])
220 228
 
229
+@application.route("/load", methods=['GET', 'POST'])
230
+def load():
231
+    if request.method=="POST":
232
+        filename = request.form["filename"]
233
+        try:
234
+            moment = json.load(open("archive/{}.json".format(filename)))
235
+            session["messages"] = moment["messages"]
236
+            session["history"] = moment["history"]
237
+        except Exception as e:
238
+            flash(repr(e))
239
+        return redirect(url_for("home")+"#/oldest")
240
+    else:
241
+        return render_template("load.html",
242
+                files = [os.path.basename(path).rsplit(".",1)[0] for path in glob("archive/*.json")])
221 243
 
222 244
 @application.get("/chat-editor")
223 245
 def chat_editor():

+ 6
- 0
static/css/style.css Wyświetl plik

@@ -6,6 +6,12 @@
6 6
 	text-transform: none;
7 7
 }
8 8
 
9
+
10
+#custom-nav {
11
+	background-color: #0000003f;
12
+        padding: 4px;
13
+}
14
+
9 15
 #custom-nav a[disabled] {
10 16
 	opacity: 0.5;
11 17
 }

+ 14
- 2
templates/chat.html Wyświetl plik

@@ -21,11 +21,23 @@
21 21
                 <div id="custom-nav">
22 22
                   <!-- the zero-width-space is a tweak against tidy removing empty tags -->
23 23
                   <a href="/reset" title="Reset"><i class="fa fa-redo">​</i></a>
24
+                  <a href="/save" title="Save"><i class="fa fa-file-download">​</i></a>
25
+                  <a href="/load" title="Load"><i class="fa fa-file-upload">​</i></a>
24 26
                 </div>
25 27
 		<div class="reveal">
26 28
 			<div class="slides">
29
+                          {% with messages = get_flashed_messages() %}
30
+                            {% if messages %}
31
+				<section data-theme="solarized">
32
+                                    {% for m in messages %}
33
+                                      <h4>{{ m }}</h4>
34
+                                    {% endfor %}
35
+                                  <a href="#/latest">Continue...</a>
36
+                                </section>
37
+                            {% endif %}
38
+                          {% endwith %}
27 39
 				<section data-theme="sky">
28
-                                    <section data-background-size="cover" data-background-video="{{bg_video}}" data-background-opacity="0.75" data-background-video-loop="True" data-background-video-mute="True">
40
+                                    <section id="latest" data-background-size="cover" data-background-video="{{bg_video}}" data-background-opacity="0.75" data-background-video-loop="True" data-background-video-mute="True">
29 41
                                         <h3 class="title slide_title">{{title}}</h3>
30 42
                                         <div class="scrollable">
31 43
                                             {{ content|safe }}
@@ -36,7 +48,7 @@
36 48
                                         </form>
37 49
                                     </section>
38 50
 				    {% for moment in history %}
39
-				    <section data-background-size="cover" data-background-video="{{moment.bg_video}}" data-background-opacity="0.75" data-background-video-loop="True" data-background-video-mute="True">
51
+				    <section {% if loop.last %}id="oldest" {% endif %}data-background-size="cover" data-background-video="{{moment.bg_video}}" data-background-opacity="0.75" data-background-video-loop="True" data-background-video-mute="True">
40 52
 					    <h3 class="title slide_title">{{moment.title}}</h3>
41 53
                                             {% if moment.prompt %}
42 54
 					        <input class="prompt" value="{{moment.prompt}}" disabled />

+ 37
- 0
templates/load.html Wyświetl plik

@@ -0,0 +1,37 @@
1
+<!doctype html>
2
+<html lang="en">
3
+  <head>
4
+    <meta charset="utf-8">
5
+    <meta name="viewport" content="width=device-width, initial-scale=1">
6
+    <title>Load a moment</title>
7
+    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
8
+    <link rel="stylesheet" href=
9
+        "https://use.fontawesome.com/releases/v5.6.3/css/all.css"
10
+        integrity=
11
+            "sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
12
+            crossorigin="anonymous">
13
+  </head>
14
+  <body>
15
+    <div class="container">
16
+      <h3 class="text-center">Load a moment</h3>
17
+      <div class="row mt-4">
18
+        <div class="col-md-6 offset-md-3">
19
+          <form "card-text" method="POST" action="">
20
+    	    <select name="filename" class="form-select">
21
+    	      {% for file in files %}
22
+    	        <option value="{{file}}">{{file}}</option>
23
+    	      {% endfor %}
24
+    	      </select>
25
+	   <input type="submit" class="btn btn-success form-control" value="Load">
26
+          </form>
27
+        </div>
28
+      </div>
29
+      <div class="row mt-1">
30
+        <div class="col-md-6 offset-md-3">
31
+        </div>
32
+      </div>
33
+    </div>
34
+    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
35
+
36
+  </body>
37
+</html>

+ 1
- 1
templates/save.html Wyświetl plik

@@ -3,7 +3,7 @@
3 3
   <head>
4 4
     <meta charset="utf-8">
5 5
     <meta name="viewport" content="width=device-width, initial-scale=1">
6
-    <title>Image generator</title>
6
+    <title>Save current moment</title>
7 7
     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
8 8
     <link rel="stylesheet" href=
9 9
         "https://use.fontawesome.com/releases/v5.6.3/css/all.css"

Ładowanie…
Anuluj
Zapisz