Archive

Archive for the ‘Web’ Category

Setup Django

April 25, 2012 Leave a comment

OK, here’s the simplest way/setting for Django. I am using mac, but it should be similar for other linux.

First. Installing an official release.

  1. Download the latest release from download page.
  2. Untar the downloaded file (e.g. tar xzvf Django-X.Y.tar.gz, where X.Y is the version number of the latest release).
  3. Change into the directory created in step 2 (e.g. cd Django-X.Y).
  4. If you’re using Linux, Mac OS X or some other flavor of Unix, enter the command sudo python setup.py install at the shell prompt.

After install it, run python in the command line, and check:

import django
print django.get_version()

Second,  is to Configure your database and create your first app.

From the command line, cd into a directory where you’d like to store your code, then run the following command:

django-admin.py startproject mysite
Since you install it through command line, ‘django-admin.py’ should be in your system path. If you can not find it, try to find it in site-packages/django/bin, where site-packages is a directory within your Python installation. Consider symlinking to django-admin.py from some place on your path, such as /usr/local/bin.
The above command will create your first site like this:
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Now run the server by doing:

python manage.py runserver.

You will see somthing like this in your terminal:

Validating models...
0 errors found.Django version 1.4, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
 goto http://127.0.0.1:8000/,  cool, it works.  Please also note that MAC actually comes with a embedded Apache Server, you may like to check in details here.
Now you needs to configure you database. Edit mysite/settings.py. To make it as simple as possible, use sqlite3 ( it goes with python, so you don’t have to install additional packages) Change the ENGINE from ‘django.db.backends.‘ to ‘django.db.backends.sqlite3‘.
OK, you are now ready for creating your own models 🙂
Categories: Python, Web Tags: ,

Site for favicon.ico

April 17, 2012 Leave a comment

This is where you can make your ico file : http://www.favicon.cc/

Ico file is the icon that displays in the address bar of your web browser when you connect to a website.

Categories: Web

Fix Matplotlib MPLCONFIGDIR error

April 17, 2012 Leave a comment

When using Apache + python-cgi, I encounter this error:

<type ‘exceptions.RuntimeError’>: Failed to create /$dirstring$/common/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data 

An official explanation of MPLCONFIGDIR is here.

MPLCONFIGDIR This is the directory used to store user customizations to matplotlib, as well as some caches to improve performance. If MPLCONFIGDIR is not defined, HOME/.matplotlib is used by default.

It turns out that on my windows machine HOME/.matplotlib is now writable. Here’s a way to simply bypass this problem.

Find out the place where you first import matplotlib, right before it, set the $HOME dir to a temporary dir where it’s writable:

For example, I was importing matplotlib in an indirect way through “from scipy.cluster.vq import kmeans2“, so right before it, do

os.environ[‘HOME’] = r’C:\……\Apache2.2\htdocs\tmp’

from scipy.cluster.vq import kmeans2

Setup Apache Server on MAC

April 13, 2012 1 comment

To Set Up a Server on my mac.

  • Goto Preference: Sharing, Check the Sharing View
  • Now the apache is running, to do a test: put:  localhost in your browser, you will see “it works!”
  • When you check the box for Sharing View, you will be notified that there are two different websites are now available on your computer
    • 2. Computer Website: http://your-computer-name/
    •       The Site Folder in  <Library/WebServer/>
    •       There are 3 subfolders under this folder: a) CGI-Executable; b) Documents; c) Share
    •        You should put your executables in this folder, such as the below python code:

#!/usr/bin/python
print “Content-type: text/html”
print
print “<html><head>”
print “”
print “</head><body>”
print “Test Page”
print “</body></html>”

Name it as ‘test.py’, and in the terminal change it to

                   $ sudo chmod 755 test.py

Now you can run it by : http://localhost/cgi-bin/test.py

Here’s a useful link: http://www.editrocket.com/articles/python_apache_mac.html

Also here’s how to share a folder: http://www.macworld.com/article/1140370/pythonserver.html

In Terminal, first cd to the folder you’d like to make available via the Web server. For example, cd ~/Pictures to switch to your user’s Pictures folder. Next, you activate Python’s built-in Web server with this command: python -m SimpleHTTPServer (and yes, capitalization counts). Press Return, and you’ll see a message stating Serving HTTP on 0.0.0.0 port 8000 .... The important bit there is the port number (8000). (You can use a different port number by simply including it at the end of the command, like so: python -m SimpleHTTPServer 8080.) To access the server from the machine you’re running the Python server on, switch to a Web browser, and enter http://localhost:8000 (replace 8000 with the port you used, if you didn’t use the defaut) in the URL bar. When you press Return, you’ll see a listing of all files in the directory. Depending on what types of files are there, and which browser you’re using, you can then click on files to view them in the browser. In my testing, Firefox works better than Safari, as Safari wants to download file types it doesn’t know how to handle while Firefox will often let you open them with a helper application. To access the server from another machine on your network, you need to know the server computer’s name. You can find this in the Sharing System Preferences panel, in the aptly-named Computer Name section of the panel. Just below the computer’s actual name, you’ll see a line that begins Computers on your local network can access…, followed by the name they’ll need to use to do so. Typically, this is simply the Computer Name followed by .local—so if the machine’s name is OctoMac, for instance, you can then access the server by entering http://OctoMac.local:8000 in your browser’s URL bar.