Browse Source

added image stream.

Signed-off-by: Nicholas Tan Jerome <nicholas.jerome@kit.edu>
Nicholas Tan Jerome 6 years ago
parent
commit
4cc3e6cab5
4 changed files with 86 additions and 14 deletions
  1. 61 11
      index.py
  2. BIN
      static/.DS_Store
  3. 13 0
      static/css/style.css
  4. 12 3
      templates/index.html

+ 61 - 11
index.py

@@ -1,19 +1,43 @@
 #coding=utf-8
 import os
-
+import io
+from PIL import Image
+from HTMLParser import HTMLParser
 import tornado.httpserver
 import tornado.ioloop
 import tornado.options
 import tornado.web
-
 from tornado.options import define, options
 define("port", default=8000, help="run on the given port", type=int)
 
 
+PATH=os.path.dirname(os.path.abspath(__file__))
+
+
+class MyHTMLParser(HTMLParser):
+    def __init__(self):
+        self.reset()
+        self.ttype = "p"
+        self.fed = []
+    
+    def handle_starttag(self, tag, attrs):
+        self.ttype = str(tag)
+
+    def handle_endtag(self, tag):
+        self.ttype = "p"
+
+    def handle_data(self, data):
+        self.fed.append({"type": self.ttype, "text": data.strip()})
+        
+    def get_data(self):
+        return self.fed
+
+
 class Application(tornado.web.Application):
     def __init__(self):
         handlers = [
             (r"/", MainHandler),
+            (r"/img", ImageHandler),
             (r"/download", GenFileStreamerHandler),
         ]
         settings = dict(
@@ -25,8 +49,12 @@ class Application(tornado.web.Application):
 
 
 class MainHandler(tornado.web.RequestHandler):
+
     def get(self):
         
+        parser = MyHTMLParser()
+        parser.feed("<i>Xenomorphia resurrecta</i>, male; well-preserved, head hollow")
+        
         self.render(
             "index.html",
             title = """Preservation of three-dimensional anatomy in phosphatized 
@@ -46,26 +74,48 @@ class MainHandler(tornado.web.RequestHandler):
                 "Saab",
                 "Mercedes",
                 "Audi"],
-            desc = "<i>Xenomorphia resurrecta</i>, male; well-preserved, head hollow",
+            desc = parser.get_data(),
             data_size = "1.3 GB"
         )
 
 
 class GenFileStreamerHandler(tornado.web.RequestHandler):
-    # 0.5 MB
     CHUNK_SIZE = 512000
-    #def initialize(self, file_path):
-    #    self.path = file_path
+
     @tornado.web.asynchronous
     @tornado.gen.engine
     def get(self):
         #print self.get_arguments("did")
-        self.path = '/Users/ntj/Development/fossil/data/01_NRM-PZ_Ar65716/test.zip'
+        self.path = PATH + '/data/01_NRM-PZ_Ar65716/test.zip'
         file_name = "01_NRM-PZ_Ar65716.zip"
-        #self.set_header("Content-Type", "text/plain")
-        self.set_header('Content-Type', 'application/octet-stream')
-        self.set_header('Content-Disposition', 'attachment; filename=' + file_name)
-        self.set_header("Content-Length", os.path.getsize(self.path))
+        self.set_header('Content-Type',
+                        'application/octet-stream')
+        self.set_header('Content-Disposition',
+                        'attachment; filename=' + file_name)
+        self.set_header("Content-Length",
+                        os.path.getsize(self.path))
+        self.flush()
+        fd = open(self.path, "rb")
+        data = fd.read(self.CHUNK_SIZE)
+        while data:
+            self.write(data)
+            yield tornado.gen.Task(self.flush)
+            data = fd.read(self.CHUNK_SIZE)
+        fd.close()
+        self.finish()
+
+
+class ImageHandler(tornado.web.RequestHandler):
+    CHUNK_SIZE = 512000
+
+    @tornado.web.asynchronous
+    @tornado.gen.engine
+    def get(self):
+        #print self.get_arguments("did")
+        self.path = PATH + '/static/data/1.png'
+        self.set_header('Content-Type', 'image/png')
+        self.set_header("Content-Length",
+                        os.path.getsize(self.path))
         self.flush()
         fd = open(self.path, "rb")
         data = fd.read(self.CHUNK_SIZE)

BIN
static/.DS_Store


+ 13 - 0
static/css/style.css

@@ -293,6 +293,10 @@ hr {
     margin-right: 10px;
 }
 
+#text-desc i {
+    font-style: italic;
+}
+
 #details span {
     color:#666;
     font-weight:bold;
@@ -302,3 +306,12 @@ hr {
 #details p {
     margin: 15px 0 0 15px;
 }
+
+#download {
+    background-color: white;
+    transition: background-color 0.5s ease;
+}
+
+#download:hover {
+    background-color: #ddd;
+}

+ 12 - 3
templates/index.html

@@ -56,7 +56,7 @@
                 <option value={{ i+1 }}>{{ i+1 }}_{{ sample_name }}</option>
             {% end %}
         </select>
-        <img class="icon" src="{{ static_url("img/icon/download.svg") }}"/>
+        <img class="icon" id="download" src="{{ static_url("img/icon/download.svg") }}"/>
         <span>({{ data_size }})</span>
     </div>
 
@@ -68,7 +68,7 @@
                 <img class="icon" src="{{ static_url("img/icon/right-arrow.svg") }}"/>
             </div>
 	        <div class="photowrapper">
-                <img src="{{ static_url("data/1.png") }}"/>
+                <img src="/img"/>
             </div>
 	        <div id="slider-left"></div>
         </div>
@@ -100,7 +100,16 @@
     
     <div id="text-block">
         <div id="text-desc">
-            <p><span>Description</span>{{ desc }}</p>
+            <p><span>Description</span>
+                {% for text_desc in desc %}
+                    {% if text_desc['type'] == "i" %}
+                        <i>{{ text_desc['text'] }}</i>
+                    {% else %}
+                        {{ text_desc['text'] }}
+                    {% end %}
+                {% end %}
+                
+            </p>
         </div>
         <div id="details">
             <p><span>Overview</span></p>