I am working on a web application that use Python => (server) and Django => (client):
I would like to know how to make (write) a client for Django; example: when i want to display a list I must send a request to the server and then display it on my web page. do you have any ideas to make that?? Thank you
If you're trying to get Django, while processing a view, to make an HTTP request to another server, you'll need to use either httplib on Python 2 or http.client on Python 3.
The Django Tutorial goes over templates.
Further, if you are trying to retrieve data without loading the page, AJAX is what you are looking for. Here are some AJAX references for Django.
Let us know if you have a specific question about a Django page.
Related
I have a site made in wordpress and i´d like to show my django application inside this site. I don´t know how to do that. How do call my django application and show it inside the site.
Your other option - far more work and technical than using an iframe - is to show the content in the WordPress site by using django-rest-framework (on the Django app) then pulling in the content via API requests.
It would be a lot of work, but well worth learning and provide you the most flexibility.
The steps would be
Install django-rest-framework for your Django app
Setup up and configure an API
Either use PHP on the server or JavaScript on the frontend to pull in the content from the API
Only using iframes, because they bore are diffrerent frameworks using different languages etc... so you will make to make one iframe inside your wordpress page and inside that page to load one url from your django application
https://www.w3schools.com/tags/tag_iframe.asp
try to detail your issue. Do you need a link in your wordpress´site ? have you tried loading it using iframe ?
It´s really important to show inside a wordpress´site ?
Currently I have written a python script that extracts data from flickr site, and dump as a python class object and YAML file.
I was planning to turn this into a website:
and a simple html front page that can send a request to the backend, triggers the python scripts running on the backend
the response will be parsed and render as a table on the html page.
As i am very new to python, I am not sure how to plan my project. is there any suggestions for building this application? any framework or technologies i should use? For example, i guess i should use ajax in the front end, how about the backend?
Thanks in advance for your suggestions!
You can look on Django or Flask.
As for me, Flask is much better for you, because it smallest and simplier. You can read about building Flask app here:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
And about Django here:
http://www.djangobook.com/en/2.0/index.html
I used both for building applications with AJAX.
Please note that Django has better Python 3 support.
Also you can see on Bottle or something other. Try google if you'll dislike Django and Flask. For example, comparison of Python web frameworks:
http://www.sixfeetup.com/blog/4-python-web-frameworks-compared
you should use Django framework for your app. You can integrate your scripts into Django views. And you can also use the loaddata system in order to insert your yaml data into the database
I have a question about using Flask with Python.
Lets say I want to make a website for some mod I'm making for a game, and I want to put in a live chat feed, how would I go around modifying the contents of the page after the page has been sent to the person?
That is definitely doable. What you do is load an html page with some javascript that will make calls to your webserver to update the page with recent chat lines. The tutorial Realtime Web Chat with Socket.io and Gevent is a very good place to start. He explains the whole process from the ground up in one article. There are also two other stackoverflow questions that might be useful to you:
A tutorial for a web-based chat server in Python
Python + comet + web (chat) - I need working(!) library/server
For flask specific implementations of a chat application you might want to check out these two projects:
flask-gevent-socketio-chat
flask-chat
Short answer: you can't.
Longer answer: once you have "sent the page" (that is, you have completed a HTTP response) there is no way for you to change what was sent. You can, however, use JavaScript to make additional HTTP requests to the server, and use the HTTP responses to modify the DOM which will change the page that the person is looking at. There are many ways to make a live chat feed, all of which are too complicated to put in a single Stack Overflow answer, but you can be sure that they all use JavaScript.
I would suggest you look into AJAX, specifically jQuery.
jQuery can send ajax request to your flask backend to retrieve more data and upon fetching new data it can modify the html contents of the page in user's browser.
Example:
$.getJSON("/chat-feed", function(msg){
$("#chat-container").append("<div>" + msg.text + "</div>");
});
I use https://github.com/mrjoes/sockjs-tornado for a Django app. I can send messages from javascript console very easy. But I want to create a signal in Django and send json string once the signal is active.
Could anyone give me a way to send a certain message in Python to sockjs-tornado socket server?
There are few options how to handle it:
Create simple REST API in your Tornado server and post your updates from Django using this API;
Use Redis. Tornado can subscribe to the update key and Django can publish updates to this key when something happens;
Use ZeroMQ (AMQP, etc) to send updates from the Django to the Tornado backend (variation of the 1 and 2).
In most of the cases, it is either first or second option. Some people prefer using 3rd option though.
I've wrote djazator, simple and easy to use django plugin. It uses zeromq for delivering messages from django to sockjs-tornado. Additionally, it can send messages to subset of authenticated django users.
I just put this up https://github.com/amagee/sockjs-client for talking directly to a SockJS server from Python (using xhr streaming).
What is a good/simple way to create, say a five page wizard, in Python, where the web server component composes the wizard page content mostly dynamically by fetching the data via calls to a XML-RPC back-end. I have experienced a bit with the XML-RPC Python module, but I don't know which Python module would be providing the web server, how to create the static content for the wizard and I don't know how to extend the web server component to make the XML-RPC calls from the web server to the XML-RPC back-end to be able to create the dynamic content.
If we break down to the components you'll need, we get:
HTTP server to receive the request from the clients browser.
A URL router to look at the URL sent from client browser and call your function/method to handle that URL.
An XML-RPC client library to fetch the data for that URL.
A template processor to render the fetched data into HTML.
A way to send the rendered HTML as a response back to the client browser.
These components are handled by almost all, if not all, Python web frameworks. The XML-RPC client might be missing, but you can just use the standard Python module you already know.
Django and Pylons are well documented and can easily handle this kind of project, but they will also have a lot of stuff you won't need. If you want very easy and absolute minimum take a look at using juno, which was just released recently and is getting some buzz.
These frameworks will handle #1 and provide a way for you to specify #2, so then you need to write your function/method that handles the incoming request (in Django this is called a 'view').
All you would do is fetch your data via XML-RPC, populate a dictionary with that data (in Django this dictionary is referred to as 'context') and then render a template from the context into HTML by calling the template engine for that framework.
Your function will just return the HTML to the framework which will then format it properly as an HTTP response and send it back to the client browser.
Simple!
UPDATE: Here's a description of how to do wizard style multiple-step forms in Django that should help you out.