url: https://github.com/mw10104587/toy_jp
- Build Session manager to communicate with market and client at the same time. This requires using websockets to communicate with both sides. According to this link, the new Channels might be a good solution.
Another potential Resources: django-socket.io
After you start running this application using
python manage.py runserver
You can access our javascript test page of QUnit by going to the page
http://localhost:8000/dashboard/test/
This application uses socket to send message between the client side(trader) and the server side that manages multiple order requests. Data are sent by using JSON format.
In order to launch this project successfully, please make sure that you have Postgres installed. You also have to create a db, a user and grant access to it. The follow are the code you might need. These are for mac OSX but there shouldn't be much difference except the first part.
Start the Database server by using
1. postgres -D /usr/local/pgsql/data
or
2. pg_ctl -D /usr/local/pgsql/data -l logfile start
Launch the psql console
psql postgres
Generate database and user and grant access
postgres=# CREATE DATABASE ase4156;
postgres=# CREATE USER ase4156_user WITH PASSWORD '000000';
postgres=# grant all privileges on database ase4156 to ase4156_user;
exit psql console.
Migrate data base
python manage.py migrate
// Note that the path doesn't matter for routing; any WebSocket
// connection gets bumped over to WebSocket consumers
socket = new WebSocket("ws://" + window.location.host + "/chat/");
socket.onmessage = function(e) {
$("body > div").append(e.data + "\n");
}
socket.onopen = function() {
socket.send("hello world");
}
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();
Type in the following command at the project root directory.
// use virtual environment
$ source myprojectenv/bin/activate
python manage.py runserver 0.0.0.0:8000
We have several frameworks that neede to be connected. We decide to use Django, MySQL and d3.js to build our application and show data and process to our users. Our main task is to link all of them together. Nothing was particularly hard, but it took us a while to understand the structure of Django and connecting it to MySQL server. MySQL Server also took us a while setting up. The following are the problems we encountered and we take down how we solved these problems.
This part might be different for everyone.
/usr/local/Cellar/mysql/5.7.10/bin/mysql.server start
Create a root user before you log in REF: http://stackoverflow.com/questions/11760177/access-denied-for-root-user-in-mysql-command-line
user: root
password: password
A) Create a database
DATABASE NAME: toy_jp
mysql> CREATE DATABASE toy_jp CHARACTER SET UTF8;
Query OK, 1 row affected (0.00 sec)
B) Create a user and grant access to the database
mysql> CREATE USER mw10104587@localhost IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON toy_jp.* TO mw10104587@localhost;
Query OK, 0 rows affected (0.01 sec)
- Create Virtual Environment for Python
- Install django mysql
- Migrate the database
- Create super user
(myprojectenv)Chi-An🀄️ toy_jp $python manage.py createsuperuser
Username (leave blank to use 'wangchi-an'):
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
A) switch to the right database
USE toy_jp;
B) CREATE TABLE
watch out for datatype
C) Add content
INSERT INTO orders (company_name, quantity) VALUE ("Starbucks", 100);
REF: https://docs.djangoproject.com/en/1.10/topics/db/queries/
The Order
model could be found in orders/models.py
. It will be included in dashboard for future database access.
REF: http://stackoverflow.com/questions/14400035/how-to-return-a-static-html-file-as-a-response-in-django
REF: https://tutorial.djangogirls.org/en/html/
A) Generated another app called the dashboard, in the dashboard I put all of my related templates in /dashboard/templates/dashboard
B) Set up views.py by adding
def dashboard(request):
# get data here
orders = Order.objects.filter(company_name="Apple")
t = orders[0].json_object()
return render(request, 'dashboard/dashboard.html', {'apple': t})
This function will be called and the specified html file will be rendered. Things to note
- setup the global url under toy_jp/urls.py
- variables could be passed into the template by the thrid variable in the
render
function. In the template, just use it by the key value and wrap them up with two curly brackets. E.G.{{key}}
By putting the curly brackets in the <script></script>
in template, the variables will be rendered while the request was sent out by our Django server. While the browser execute the javascript in the html file, the data is already there. There might be concerns about this. In the future, if the data gets bigger, we want to provide asynchronous loading solution for this.
After adding data, command line showing what's in the database orders_order
, which means the order
module under orders
app.
We try to remove data, and reloaded the page, and every worked like charm.
- Consider using django-nvd3 framework to develop reusable templates. REF:https://github.com/areski/django-nvd3
- Include Bootstrap into our project
Our teammates half are using mac book and half are using windows, I believe we also need to make sure the compatibility of Python version, mysql settings.
Outputting csv file with Django: https://docs.djangoproject.com/en/1.10/howto/outputting-csv/
- Whether to make an api to request data into template, or to use api, or both...
- How to link static files including css and other js libraries.
- Whether to use
gulp
as project management software - Whether to use CommonJS to control all of our js files