Browse Source

added sqlite as information db.

Signed-off-by: Nicholas Tan Jerome <nicholas.jerome@kit.edu>
Nicholas Tan Jerome 6 years ago
parent
commit
4c17458348
5 changed files with 102 additions and 30 deletions
  1. BIN
      fossil.db
  2. 52 27
      index.py
  3. 41 0
      res/fossil_reader.py
  4. 6 0
      static/js/script.js
  5. 3 3
      templates/index.html

BIN
fossil.db


+ 52 - 27
index.py

@@ -1,8 +1,10 @@
-#coding=utf-8
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
 import os
 import io
 from PIL import Image
 from HTMLParser import HTMLParser
+import sqlite3 as lite
 import tornado.httpserver
 import tornado.ioloop
 import tornado.options
@@ -27,7 +29,8 @@ class MyHTMLParser(HTMLParser):
         self.ttype = "p"
 
     def handle_data(self, data):
-        self.fed.append({"type": self.ttype, "text": data.strip()})
+        self.fed.append({"type": self.ttype,
+                         "text": data.strip()})
         
     def get_data(self):
         return self.fed
@@ -49,11 +52,29 @@ class Application(tornado.web.Application):
 
 
 class MainHandler(tornado.web.RequestHandler):
+    
+    #def __init__(self):
+    #    con = lite.connect('fossil.db')
+        #with con:
+        #    cur = con.cursor() 
 
     def get(self):
         
+        con = lite.connect('fossil.db')
+        with con:
+            cur = con.cursor()
+            cur.execute("SELECT * FROM Fossil ORDER BY id")
+            data = cur.fetchall()
+        
+        data_list = [ [x[0], x[1]] for x in data]
+
+        current_index = 0
         parser = MyHTMLParser()
-        parser.feed("<i>Xenomorphia resurrecta</i>, male; well-preserved, head hollow")
+        print data[current_index][2]
+        parser.feed(data[current_index][2])
+        desc = parser.get_data()
+        
+        data_size = ("%.2f" % (data[current_index][3] / 1000000.0))
         
         self.render(
             "index.html",
@@ -69,39 +90,43 @@ class MainHandler(tornado.web.RequestHandler):
                 Institute of Crop Science and Resource Conservation, University of Bonn, Bonn, Germany; 
                 Laboratory for Applications of Synchrotron Radiation, Karlsruhe Institute of Technology, 
                 Karlsruhe, Germany""",
-            data_list = [
-                "NRM-PZ_Ar65716",
-                "Saab",
-                "Mercedes",
-                "Audi"],
-            desc = parser.get_data(),
-            data_size = "1.3 GB"
+            data_list = data_list,
+            desc = desc,
+            data_size = data_size
         )
 
 
 class GenFileStreamerHandler(tornado.web.RequestHandler):
     CHUNK_SIZE = 512000
-
+    DATA_PATH = "/home/ntj/fossils_for_nicholas"
+    
     @tornado.web.asynchronous
     @tornado.gen.engine
     def get(self):
-        #print self.get_arguments("did")
-        self.path = PATH + '/data/01_NRM-PZ_Ar65716/test.zip'
-        file_name = "01_NRM-PZ_Ar65716.zip"
-        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)
+        cdid = self.get_arguments("did")
+        con = lite.connect('fossil.db')
+        with con:
+            cur = con.cursor()
+            cur.execute("SELECT name FROM Fossil WHERE name = ?", (cdid))
+            data_key = cur.fetchall()
+            
+        if len(data_key) > 0:
+            file_name = str(cdid[0]) + ".zip"
+            self.path = self.DATA_PATH + str(cdid[0]) + "/" + file_name
+            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)
-        fd.close()
+            while data:
+                self.write(data)
+                yield tornado.gen.Task(self.flush)
+                data = fd.read(self.CHUNK_SIZE)
+            fd.close()
         self.finish()
 
 

+ 41 - 0
res/fossil_reader.py

@@ -0,0 +1,41 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+import sqlite3 as lite
+import sys
+import os
+from sh import du
+
+
+directory = "/mnt/LSDF/ASTOR/fossils_for_nicholas/"
+con = lite.connect('fossil.db')
+
+with con:
+    cur = con.cursor()    
+    for folder in os.listdir(directory):
+        with open(directory+folder+'/'+folder+'.txt', 'r') as myfile:
+            data=myfile.read().replace('\n', '')
+
+        fsize = du(directory+folder+"/slices/")
+        fsize = fsize.stdout.split("\t")[0].strip()
+    
+        index = int(folder[-5:])
+        name = str(folder)
+        desc = data
+        size = fsize
+        cur.execute("INSERT INTO Fossil VALUES (?, ?, ?, ?);", (index, name, desc, size)) 
+
+"""
+#CREATE TABLE
+con = lite.connect('fossil.db')
+with con:
+    cur = con.cursor()    
+    cur.execute("CREATE TABLE Fossil(Id INT, Name TEXT, Description TEXT, Size INT)")
+    #cur.execute("INSERT INTO Cars VALUES(1,'Audi',52642)")
+    #cur.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)")
+    #cur.execute("INSERT INTO Cars VALUES(3,'Skoda',9000)")
+    #cur.execute("INSERT INTO Cars VALUES(4,'Volvo',29000)")
+    #cur.execute("INSERT INTO Cars VALUES(5,'Bentley',350000)")
+    #cur.execute("INSERT INTO Cars VALUES(6,'Citroen',21000)")
+    #cur.execute("INSERT INTO Cars VALUES(7,'Hummer',41400)")
+    #cur.execute("INSERT INTO Cars VALUES(8,'Volkswagen',21600)")
+"""

+ 6 - 0
static/js/script.js

@@ -29,4 +29,10 @@ $(function() {
             console.log(ui.value);
         }
     });
+    
+    $( "#download" ).click(function(e) {
+        var did = $( "select option:selected" ).text();
+        e.preventDefault();  //stop the browser from following
+        window.location.href = 'download?did=' + did;
+    });
 });

+ 3 - 3
templates/index.html

@@ -52,12 +52,12 @@
     <div id="subheader">
         <span>Sample:</span> 
         <select style="width: auto;">
-            {% for i, sample_name in enumerate(data_list) %}
-                <option value={{ i+1 }}>{{ i+1 }}_{{ sample_name }}</option>
+            {% for sample in data_list %}
+                <option value={{ sample[0] }}>{{ sample[1] }}</option>
             {% end %}
         </select>
         <img class="icon" id="download" src="{{ static_url("img/icon/download.svg") }}"/>
-        <span>({{ data_size }})</span>
+        <span>({{ data_size }} GB)</span>
     </div>
 
     <div id="image-container" class="clearfix">