Web based wizard with Python - python

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.

Related

How to communicate data from ReactJS and Python?

I'm building an app in which I need to communicate data from and to ReactJS and Python. It needs to go both ways - but I'm more concerned about the React part right now. At first I considered JSON, but couldn't find any resources/ libraries to update JSON files, and people said I should stay away from that. Other than literally creating a text file with data in it, what are my options? The application has to do with getting stock data from a python API, doing calculations on it, and sending the data to ReactJS to render on a webpage. I also need ReactJS to send account data back to Python where we do our MySQL. Any suggestions?
Since the ReactJS app is a front-end application, your only real choice to ensure security is exposing an API on the python app which the ReactJS app talks to (via Websockets for example since you're mentioning a bidirectional communication). Maybe take a look at something like Flask with the flask-socketio package.

Django and React

I haven't been able to locate anything saying one way or the other, but does anyone know if you can have function based views in django and have react as your frontend? No use of templates.
If so, would it look like this?
# fad index route
def fad_index(request):
context = {'fads': Fad.objects.all()}
return render(request, 'fad/fad_index.html', context)
But instead of fad_index.html, would it just be the route that you want it to take in react?
When you use Django views, whether class or function based, you are required to render html responses, so there is no point on using React this way since the front-end functionality is already embedded in the html you are rendering.
React is thought for creating front-end applications that run independently from the back-end application. For that, the back-end application must provide specific url points for fetching or posting data from and to the front-end. These url points are known as end-points, and this back-end architecture is known as API, and come in two main flavors, SOAP and REST, and differ, basically, in the format of the data that comes from and goes to the front-end. SOAP APIs use xml, and REST APIs use json.
If you have a REST API back-end, you can use ajax in a React front-end to fetch and post data from and to the back-end's end-points.
For making a REST API back-end using Django, you need to complement it with another library called Django Rest Framework, that allows Django views to render json packages instead of html, so your React front-end can use these end-points.
React is simply a component composition library. You can use it to render single components on a web page or as the rendering layer of a more complex Single Page Application (SPA)
The general best practice when creating single page applications is to send the data over in a JSON format using a REST API. JSON because it can be parsed in almost any language and REST because the server should not care what the client is specifically doing.
There is a module called django rest framework that allows you to return raw data from REST style endpoints in a JSON format.

how do I create a client in django?

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.

How to retrieve information from a Django template

I have a django template that calls a python methods but I can't seem to find a way for the python method to retrieve information from the template such as user input. Could anybody tell me how to do this?
I think you're under a misconception (A reasonably common one) that because you can use django variables in the html template, the template can "send" information back to django/the database.
Instead, try to get into the habit of thinking in terms of request/response. The user requests a web page, the django server builds up the response using content from the database and an html template (via the template language) and serves it to the user. If the response page has a form in it, that's not the "template" sending information back to the Django server, that's the response web page providing the user will tools to make ANOTHER request (this time a POST request).
The template is merely a generic container that you fill up with your dynamic content to build up the entire HTTP response.

How to append the #id in tornado url?

I am currently working on the WhiteBoard application. I need to append the #id to url. it is same as http://awwapp.com/draw.html#1205febd. I am new to tornado framework. How to call this url to view. Please explain me. Thanks.
#1205febd is not about back-end. It's a so-called target. When you have a link like <a href="#1205febd"> on page http://awwapp.com/draw.html browser will append that hash to URL automatically (on click).
If you want to perform some actions on back-end depending on that hash you should definitely to get the id on front-end and use some RIA framework (JQuery, AngularJS etc.) to send AJAX queries to your back-end (Tornado?) and render the layout in browser depending on its response.

Categories

Resources