Update webpage based on sql database - python

As a non webdev, I'd really like if you could point me out to the 'correct' way to do this.
I built an application that populates and updates a database periodically (sqlite to be precise). I store leaderboards in my database, and would like to display them in a webpage.
As my leaderboards can change all the time, I need the webpage to be dynamic, either on a time based trigger or whenever the database changes.
I was about to use javascript for that, by realized that my database is hosted on my server, so client side js might not work.
Any ideas on that?
As my app is built in Python, I'd prefer avoid using php solutions but use more 'trendy' technologies (js, ruby, python, ? ? ? whatever)
Thanks!
Ok, given the keywords I got now here is an almost exact duplicate :
Notify user on database change? JavaScript/AJAX

This is what Ajax is for. You write Javascript on the front end that uses Ajax to call your server-side code to return the database content, then updates your HTML when it receives a response.

You need to use javascript on the client side not on the server. Your javascript code will make async calls (using ajax) to check if the db changed and update the website accordingly.

You can either use javascript to poll the server side periodically. Or you can use javascript to create (perhaps using a library like SockJS or SocketIO) a websocket connection that can actually push data to the client side when it changes. I do this on a number of projects using Tornado's websocket support on the server side.

Related

Automatically Detect Websocket Path (Firebase)

Example URL = https://westgate-production-4cb87.firebaseapp.com/super-contests/weekly-card/embed
I've written the code needed to get this to work with the current deployed websocket URL and successfully scrape the data on this page -
wss://s-usc1c-nss-276.firebaseio.com/.ws?v=5&ns=westgate-production-4cb87
However, it seems that every so often the number in the subdomain will change (in this example - 276). I can obviously manually figure out what the new URL is using the Network tab in Dev Tools, but I was wondering if there was a Python code snippet or some approach where I can programatically detect what websockets are opening so that I can capture the wss:// URL and then pass it into the rest of my code that works correctly.
Thank you!
You're using an undocumented way to access the Firebase Realtime Database.
That first part of the URL is the server that your client is connecting to to access the data, and is actually determined by the first request(s) when the connection is established. But as said, this knowledge is not documented and can thus change at any time without notice.
I'd recommend using either the Python SDK, the REST API, or the REST Streaming API to access the database, as all of those are properly documented.

How do you pass variables using a HTTP post from a JS file/function to a separate python file/function hosted somewhere else?

I have JS running and essentially getting user entries from my HTML session storage and pushing these to a DB. I also need to use a HTTP request to pass a json object containing the entries to a python file hosted somewhere else.
Does anyone have any idea of documentation I could look at, or perhaps how to get JSON objects from JS to Python.
My client does not want me to grab the variables directly from the DB.
You have to create some sort of communication channel between the javascript and python code. This could be anything, SOAP, HTTP, RPC, any number of and flavor of message queue.
If nothing like that is in place, it's quite the long way around. A complex application might warrant you doing this, think micro services communicating across some sort of service bus. It's a sound strategy and perhaps that's why your client is asking for it.
You already have Firebase, though! Firebase is a real-time database that already has many of the characteristics of a queue. The simplest and most idiomatic thing to do would be to let the python code be notified of changes by Firebase: Firebase as service bus is a nice strategy too!

Asynchronous way to listen web service for data change

I am new to python. I want to read JSON data from URL and if there is any change in JSON data on server, I want to update in my JSON file which is on client. How can I do that through python?
Actually i am ploting graph on django using JSON data which is on another server. That JSON data is updated frequently. So here i want to update my charts based on updated json data. For that i has to listen to URL link for change. So how can i do that.....i know with select() system call i can but need some another way
There's no way to "listen" for changes other than repeatedly requesting that URL to check if something has changed; i.e. classic "pull updates". To get actual live notifications of changes, that other server needs to offer a way to push such a notification to you. If it's just hosting the file and is not offering any active notifications of any sort, then repeatedly asking is the best you can do. Try not to kill the server when doing so.

Creating a python web server to recieve XML HTTP Requests

I am currently working on a project to create simple file uploader site that will update the user of the progress of an upload.
I've been attempting this in pure python (with CGI) on the server side but to get the progress of the file I obviously need send requests to the server continually. I was looking to use AJAX to do this but I was wondering how hard it would be to, instead of changing to some other framerwork (web.py for instance), just write my own web server for receiving the XML HTTP Requests?
My main problem is that sending the request is done from HTML and Javascript so it all seems like magic trickery at the moment.
Can anyone advise me as to the best way to go about receiving these requests on the server?
EDIT: It seems that a framework would be the way to go. Would web.py be a good route to take?
I would recommend to use a microframework like Sinatra for Ruby. There seem to be some equivalents for Python. What python equivalent of Sinatra would you recommend?
Such a framework allows you to simply map a single method to a route.
Writing a very basic HTTP server won't be very hard (see http://docs.python.org/library/simplehttpserver.html for an example), but you will be missing many features that are provided by real servers and web frameworks.
For your project, I suggest you pick one of the many Python web frameworks and run your application behind Apache/mod_wsgi.
There's absolutely no need to write your own web server. Plenty of options exist, including lightweight ones like nginx.
You should use one of those, and either your own custom WSGI code to receive the request, or (better) one of the microframeworks like Flask or Bottle.

How to do cloud computing with Python and Java? Final Year project

For my final year project I plan to code a cloud in Python. The client will be written in Java by the other member of my team. The client will have a tabbed interface and it will provide a text editor, a media player, a couple of small Java based games and a maybe a few more services.
The server will work like this:
1) Validate the user.
2) Send a file, called "dump" to the user. Dump will contain all the file names and file types that the user created by himself or the files which the user can read/write. This info will be fetched from the database.
3) The tabs in the client will display the file types associated with the tab application. e.g the media tab will only select and show the media files from the dump readable by user. The text editor tab will show only the txt files from the dump readable by the user.
4) A request to open the file will send the file back to client, which the associated application will open.
5) All the changes made to the files and all the actions (overwriting, saving, deleting etc.) will be sent back to the server along with the new object. Something similar will be done to the newly created objects.
My Questions are:
What are the best approaches for the communication between the client and the server. For the dump I plan to use some sort of encrypted XML file. For the other way round, I don't have a clue :/.
For easy integration with the database, I was planning to use Django (which I started few days back). How can I send my requests from the client to the server (without Django I'd use SQL queries) and the files from the server to the client? Maybe GET and POST will work for the former problem? Any other suggestions?
Q1: how should I transfer data between client/server securely
A: HTTPS to support encryption & JSON to serialise objects between languages (Python/Java) seems to be the most natural. You could experiment with XML-RPC over SSL or TSL if you want to be creative.
Q2: How do I send queries to the server's db?
A: My first response is to say talk to the person coding the server, and see what's easiest on that end. However, I think that your client should stick to HTTP. The server developer would ensure the server supports RESTful URIs. Then your client only access a URI and have the results processed by the server.
At its most raw, this could be implemented like this:
https://www.example.com/db?q="SELECT * FROM docs"
There are smarter ways to do it, but you get the idea.
If you're going to use a web framework on the server, it makes sense to use an HTTP-based protocol. The downside is that only the client can initiate a connection (e.g., the client needs to first ask for the "dump" file), but a simple GET request will suffice (remember, the server can send anything in the HTTP response, including your XML file).
Regarding encryption, it's best to use an existing protocol like HTTPS. There are well-vetted libraries that will correctly establish a secure connection between your client and the server.
Overall, I'm advocating the highest-level protocols that are appropriate for your application. HTTP(S) goes hand-in-hand with your web-based architecture, so make use of it.
Stick to Django. It's really productive. I would use JSON instead of XML. More convenient. import json. This should help you in communicating between client-server.
Also cloud computing is just a recent word that's just thrown around for (client+server+some services). Oh by the way all that you want to do can be completely done in Django itself. No need to go to JAVA.
Django is Cool :)

Categories

Resources