Python visualization pyecharts + Django Framework

Source: Internet
Author: User
Tags install django pip install django

Background: Based on the huge demand for visualizations and cost factors, using the Pyecharts + Django visualization is clearly a better choice
Visualize to find: patterns, relationships, and anomalies

Environment: People with obsessive-compulsive disorder have always used the latest version
django:2.1.0
python:3.x (Win10 is 3.7,ubuntu is 3.5)
Operating system environment: WIN10 and Ubuntu

1. Django Installation:
Django is a free, open-source web framework developed with Python that can be used to quickly build websites
1.1. Installing Django
I like to use Pip installation method:
Win10:pip installl Django
Ubuntu: (sudo) pip install Django

1.2, check whether the installation is successful
Enter the python environment:

Import Django
Django. VERSION
(2, 1, 0, ' final ', 0)
If you can see the corresponding version number, it proves that the installation was successful.

2. Create a new project
Ubuntu enters the following command in the terminal (Win10 is in cmd operation)
Django-admin startproject MySite #创建一个项目, named MySite
Note: If Django-admin does not work, please use django-admin.py
After success can see the following directory

A new MySite directory is created, and there is a MySite directory, which is a subdirectory mysite the setup settings.py files for some projects, the total URLs configuration file urls.py, and the wsgi.py files used to deploy the server, Init . PY is required for the directory structure of the Python package, related to the call

3. Create a new app (APP), name learn
Enter the MySite directory, not Mysite/mysite
Python manage.py Startapp Learn # Learn is the name of an app

Then, in the MySite directory, there is one more learn folder

4. Environment configuration
4.1, modify the mysite/mysite/settings.py,

This is to add the Learn file (that is, the new app) folder to the Django environment, the registration program

4.2, replace the learn/view.py,

The first line: # CODING:UTF-8 is to declare the code as Utf-8
The second line: the introduction of HttpResponse is used to return content to a Web page, like print in Python, except that the content is displayed on a Web page
About the Custom Function index (), where the first parameter must be request, related to requests sent by the Web page, which contains the contents of Get or post, the user's browser, the system and other information inside

Now the environment is set up, the page display content is also written, but how to access?
4.3, modify mysite/mysite/urls.py, add some content

The markup section is what's new
From learn import view as Learn_views: To introduce views.py script in Learn folder
Path (", Learn_views.index): Description input only IP and port number (i.e. http://127.0.0.1:8000/) execution, views.py script
Expand:
If the revision changes to

The http://127.0.0.1:8000/add0/executes the views00.py script, which is known as the interface scheme.

4.4. Run Python manage.py runserver
If it shows, that means it's successful.

Open Browser access: http://127.0.0.1:8000/
The following page will appear (because I have now deployed it to the server so I entered something else, which can be ignored here and explained later)

Now that you have deployed only native access, how will the next implementation be deployed to the server for all hosts to access
5, deploy to the server, for all host access
Deploy to the server and deploy to the local, the pre-operation is exactly the same, you can refer to the previous steps to complete 1-4 steps
But deploying to the server also requires the following work
5.1, open port, this need to open their own, assuming that the port number is 8000

5.2. Open access rights
Modify the allowed_hosts=[in mysite/mysite/settings.py] to

That is, allow all hosts to access and, of course, add only the hosts that are allowed to access
Then execute the following command under the MySite directory
Python manage.py runserver 0.0.0.0:8000
At this point in the local host input ip:8000, that is, ip+ port number, you can return the following content

The pits that have been trampled during:
1, Python2 and Python3 exist simultaneously
I use Ubuntu, which has both Python2 and Python3, and uses Python2 by default, but I need to use the default Python3
Solution:
Whereis python #找到python位置
My location is in the/usr/bin directory, so enter this directory
ll |grep python
Discover that the default Python2 is implemented via a soft connect implementation that Python points to python2, so the solution is to delete the corresponding soft connection
New Soft Connect Python--pyhton3.5

2, Error: Disallowedhostat/invalid http_host header: ' 198.211.99.20 '. Need to Addu ' 198.211.99.20 ' to allowed_hosts
This is because there is no modification to the issue caused by allowed_hosts=[in mysite/mysite/settings.py.

3, MySite The directory structure should be

There are some files due to the version of the problem, there may be differences, but MySite must have three files or directories are: Learn (app name), manage.py, MySite

4.

Follow the prompts to perform the appropriate action

Important content:
1, whether all the code must be written to the mysite/learn/views.py script, if there are multiple scripts, how to handle
solution, Xiang See step 4.1-4.3

2, if the script itself is generated HTML how to handle
That is, the pyecharts generated this is the HTML at this time how to display
Use the following framework
From django.http import HttpResponse
From Django.template.loader import get_template
Import datetime
Template = Get_template (' learn/templates/index.html ')
html = Template.render ({' Time ': now})
return HttpResponse (HTML)
Note: You need to create a new templates folder in the Learn directory to hold the generated HTML file.

Python visualization pyecharts + Django Framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.