Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Feb 4, 2016
1 parent b10487c commit 98ba323
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
9 changes: 9 additions & 0 deletions panoramix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ def get_table(self, table_name):
autoload=True,
autoload_with=self.get_sqla_engine())

def get_columns(self, table_name):

from sqlalchemy import create_engine
from sqlalchemy.engine import reflection

engine = self.get_sqla_engine()
insp = reflection.Inspector.from_engine(engine)
return insp.get_columns(table_name)

@property
def sqlalchemy_uri_decrypted(self):
conn = sqla.engine.url.make_url(self.sqlalchemy_uri)
Expand Down
7 changes: 5 additions & 2 deletions panoramix/static/panoramix.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,10 @@ var px = (function() {
}

function initSqlEditorView() {
var database_id = $('#database_id').val();
var editor = ace.edit("sql");
editor.$blockScrolling = Infinity
editor.getSession().setUseWrapMode(true);

var textarea = $('#sql').hide();
editor.setTheme("ace/theme/crimson_editor");
Expand All @@ -457,14 +459,15 @@ var px = (function() {
editor.focus();
$("select").select2({dropdownAutoWidth : true});
function showTableMetadata() {
$(".metadata").load('/panoramix/table/' + $("#dbtable").val() + '/');
$(".metadata").load(
'/panoramix/table/' + database_id + '/' + $("#dbtable").val() + '/');
}
$("#dbtable").on("change", showTableMetadata);
showTableMetadata();
$("#create_view").click(function(){alert("Not implemented");});
$(".sqlcontent").show();
$("#select_star").click(function(){
$.ajax('/panoramix/select_star/' + $("#dbtable").val() + '/')
$.ajax('/panoramix/select_star/' + database_id + '/' + $("#dbtable").val() + '/')
.done(function(msg){
editor.setValue(msg);
});
Expand Down
8 changes: 4 additions & 4 deletions panoramix/templates/panoramix/sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ <h2>db: [{{ db }}]</h2>
</div>
<div class="col-xs-5 fillheight">
<select id="dbtable">
{% for table in db.tables %}
<option value="{{ table.id }}"
{{ "selected" if table.id|string == table_id }}>
{{ table }}
{% for t in tables %}
<option value="{{ t }}"
{{ "selected" if t == table }}>
{{ t }}
</option>
{% endfor %}
</select>
Expand Down
47 changes: 32 additions & 15 deletions panoramix/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from sqlalchemy import create_engine
import sqlalchemy as sqla
from wtforms.validators import ValidationError
import pandas as pd
from sqlalchemy import select, text
from sqlalchemy.sql.expression import TextAsFrom

from panoramix import appbuilder, db, models, viz, utils, app, sm, ascii_art

Expand Down Expand Up @@ -558,33 +561,49 @@ def dashboard(self, identifier):
@expose("/sql/<database_id>/")
@utils.log_this
def sql(self, database_id):
mydb = db.session.query(models.Database).filter_by(id=database_id).first()
mydb = db.session.query(
models.Database).filter_by(id=database_id).first()
engine = mydb.get_sqla_engine()
tables = engine.table_names()
return self.render_template(
"panoramix/sql.html",
database_id=database_id,
table_id=request.args.get('table_id'),
tables=tables,
db=mydb)

@has_access
@expose("/table/<table_id>/")
@expose("/table/<database_id>/<table_name>/")
@utils.log_this
def table(self, table_id):
t = db.session.query(models.SqlaTable).filter_by(id=table_id).first()
def table(self, database_id, table_name):
mydb = db.session.query(
models.Database).filter_by(id=database_id).first()
cols = mydb.get_columns(table_name)
df = pd.DataFrame([(c['name'], c['type']) for c in cols])
df.columns = ['col', 'type']
return self.render_template(
"panoramix/ajah.html",
content=t.html)
content=df.to_html(
index=False,
na_rep='',
classes=(
"dataframe table table-striped table-bordered "
"table-condensed sql_results")))

@has_access
@expose("/select_star/<table_id>/")
@expose("/select_star/<database_id>/<table_name>/")
@utils.log_this
def select_star(self, table_id):
t = db.session.query(models.SqlaTable).filter_by(id=table_id).first()
def select_star(self, database_id, table_name):
mydb = db.session.query(
models.Database).filter_by(id=database_id).first()
t = mydb.get_table(table_name)
fields = ", ".join(
[c.column_name for c in t.columns if not c.expression] or "*")
s = "SELECT\n{fields}\nFROM {t.table_name}".format(**locals())
[c.name for c in t.get_columns()] or "*")
s = "SELECT\n{fields}\nFROM {table_name}".format(**locals())
return self.render_template(
"panoramix/ajah.html",
content=s)
content=s
)

@has_access
@expose("/runsql/", methods=['POST', 'GET'])
Expand All @@ -598,9 +617,6 @@ def runsql(self):
mydb = session.query(models.Database).filter_by(id=database_id).first()
content = ""
if mydb:
from pandas import read_sql_query
from sqlalchemy import select, text
from sqlalchemy.sql.expression import TextAsFrom
eng = mydb.get_sqla_engine()
if limit:
sql = sql.strip().strip(';')
Expand All @@ -611,9 +627,10 @@ def runsql(self):
)
sql= str(qry.compile(eng, compile_kwargs={"literal_binds": True}))
try:
df = read_sql_query(sql=sql, con=eng)
df = pd.read_sql_query(sql=sql, con=eng)
content = df.to_html(
index=False,
na_rep='',
classes=(
"dataframe table table-striped table-bordered "
"table-condensed sql_results"))
Expand Down

0 comments on commit 98ba323

Please sign in to comment.