Browse Source

Add load function

Also display flash messages if needed
master
The Dod 1 year ago
parent
commit
7ccde933ec
5 changed files with 83 additions and 6 deletions
  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 View File

1
 from flask import ( Flask, render_template, request, abort, redirect,
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
 from flask_session import Session
3
 from flask_session import Session
4
 import openai
4
 import openai
5
 from dotenv import load_dotenv
5
 from dotenv import load_dotenv
203
 @application.route("/save", methods=['GET', 'POST'])
203
 @application.route("/save", methods=['GET', 'POST'])
204
 def save():
204
 def save():
205
     if request.method=="POST":
205
     if request.method=="POST":
206
-        filename=request.form["filename"].rsplit("/",1)[-1]
206
+        filename = request.form["filename"].rsplit("/",1)[-1]
207
         if filename:
207
         if filename:
208
+            path = "archive/{}.json"
209
+            is_overwrite = os.path.isfile(path)
208
             moment = {
210
             moment = {
209
                 key: session.get(key, [])
211
                 key: session.get(key, [])
210
                 for key in ["messages", "history"]
212
                 for key in ["messages", "history"]
211
             }
213
             }
212
             print(moment)
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
         return redirect(url_for("home"))
222
         return redirect(url_for("home"))
215
     else:
223
     else:
216
         return render_template("save.html",
224
         return render_template("save.html",
218
                   "moment-%Y-%m-%d-%H.%M.%S"),
226
                   "moment-%Y-%m-%d-%H.%M.%S"),
219
                 files = [os.path.basename(path).rsplit(".",1)[0] for path in glob("archive/*.json")])
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
 @application.get("/chat-editor")
244
 @application.get("/chat-editor")
223
 def chat_editor():
245
 def chat_editor():

+ 6
- 0
static/css/style.css View File

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

+ 14
- 2
templates/chat.html View File

21
                 <div id="custom-nav">
21
                 <div id="custom-nav">
22
                   <!-- the zero-width-space is a tweak against tidy removing empty tags -->
22
                   <!-- the zero-width-space is a tweak against tidy removing empty tags -->
23
                   <a href="/reset" title="Reset"><i class="fa fa-redo">​</i></a>
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
                 </div>
26
                 </div>
25
 		<div class="reveal">
27
 		<div class="reveal">
26
 			<div class="slides">
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
 				<section data-theme="sky">
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
                                         <h3 class="title slide_title">{{title}}</h3>
41
                                         <h3 class="title slide_title">{{title}}</h3>
30
                                         <div class="scrollable">
42
                                         <div class="scrollable">
31
                                             {{ content|safe }}
43
                                             {{ content|safe }}
36
                                         </form>
48
                                         </form>
37
                                     </section>
49
                                     </section>
38
 				    {% for moment in history %}
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
 					    <h3 class="title slide_title">{{moment.title}}</h3>
52
 					    <h3 class="title slide_title">{{moment.title}}</h3>
41
                                             {% if moment.prompt %}
53
                                             {% if moment.prompt %}
42
 					        <input class="prompt" value="{{moment.prompt}}" disabled />
54
 					        <input class="prompt" value="{{moment.prompt}}" disabled />

+ 37
- 0
templates/load.html View File

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 View File

3
   <head>
3
   <head>
4
     <meta charset="utf-8">
4
     <meta charset="utf-8">
5
     <meta name="viewport" content="width=device-width, initial-scale=1">
5
     <meta name="viewport" content="width=device-width, initial-scale=1">
6
-    <title>Image generator</title>
6
+    <title>Save current 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">
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=
8
     <link rel="stylesheet" href=
9
         "https://use.fontawesome.com/releases/v5.6.3/css/all.css"
9
         "https://use.fontawesome.com/releases/v5.6.3/css/all.css"

Loading…
Cancel
Save