0

I was trying to implement a dashboard by following the instructions of https://github.com/talpor/django-dashing/ using django-dashing.

So far I have successfully customised my widget and displayed with some random data on my own web server, while I have no clue where to start if I'd like to pull some real data from DB(MySQL) and display. (like where to do the DB connecting,..etc)

Could anyone show me steps I should follow to implement it?

Sibiraj
  • 4,486
  • 7
  • 33
  • 57
loktherhal
  • 43
  • 8

2 Answers2

0

If it's still relevant, you can start by connecting to the database with sqlalchemy.

import sqlalchemy as sq
from sqlalchemy.engine import url as sq_url

db_connect_url = sq_url.URL(
            drivername='mysql+mysqldb',
            username=DB_username,
            password=DB_password,
            host=DB_hostname,
            port=DB_port,
            database=DB_name,
        )
engine = sq.create_engine(db_connect_url)

From there you can manipulate the data by checking the available methods on engine. What I normally do is use pandas in situations like these.

import pandas as pd
df = pd.read_sql_table(table_name, engine)
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Mugiwara
  • 46
  • 1
  • 4
0

I also had to do this recently.... I managed to get it sorted - but it is a little too clunky.

I created a Separate CherryPy REST Api in a separate Project. The Entry point looks like

@cherrpy.expose
def web_api_to_call(self, table,value):
     #Do SQL Query
     return str(sql_table_value)

Then in Django Created a New app, then created a Widget.py. Inside the Widget.py I wrote something like this.

import requests
class webquery(NumberWidget):
    classparams=[("widget1","web_api_to_call","table","values"),
                 ("widget2","web_api_to_call","table2","values2"),
                 ("widget3","web_api_to_call","table3","values3")]

   def myget(self):
      for tup in self.classparams:
          if tup[0]==type(self).__name__:
               url=tup[1]
               table=tup[2]
               value=tup[3]
          url = "http://127.0.0.1:8000/"+url

         # Do Web Call Error Checking Omitted
         return requests.get(url,params={"table":table,"values":value)}).text()

     def get_value(self):
        #Override default
        return self.my_get()

#Now create new Widgets as per the static definition at the top

class widget1(web query):
    id=1

class widget2(web query):
    id=1

class widget3(web query):
    id=1

You now just add your new widgets - as you normally would in the urls.py and then in the dashing-config.js and you are done.

Hope this assists someone.

Tim Seed
  • 5,119
  • 2
  • 30
  • 26