Difference between revisions of "Python Resources"

From Simson Garfinkel
Jump to navigation Jump to search
m (Building Web Applications)
m (Building Web Applications)
 
(One intermediate revision by the same user not shown)
Line 92: Line 92:
 
** Bottle (easy, and the whole thing runs from a single file)
 
** Bottle (easy, and the whole thing runs from a single file)
 
** Flask (can run on Apache with Passenger, but needs its own domain name)
 
** Flask (can run on Apache with Passenger, but needs its own domain name)
 +
** Writing your own (don't do this.)
  
 
The WSGI server needs to be run from a program that receives the Port80 connections. The two ways I've done this are:
 
The WSGI server needs to be run from a program that receives the Port80 connections. The two ways I've done this are:
  
** mod_wsgi running within Apache
+
* mod_wsgi running within Apache. (This works surprisingly well and allows you to run bottle or flask side-by-side other systems)
** CherryPy
+
* CherryPy (Includes its own web server and will hand off to Bottle or Flas.)
 
 
  
 +
For more information, see:
 
* See also https://wiki.python.org/moin/WebFrameworks
 
* See also https://wiki.python.org/moin/WebFrameworks
 
Examples and resources:
 
 
* https://github.com/MichaelPereira/webhooks-demo-application
 
* https://github.com/MichaelPereira/webhooks-demo-application
 
* https://mattcarrier.com/flask-dreamhost-setup/
 
* https://mattcarrier.com/flask-dreamhost-setup/
* https://www.byteconsole.com/get-flask-up-and-running-on-a-dreamhost-server-with-passenger/
 
  
Notes on flask:
+
If you are using anything other CGI, you need some way to tell the web server to reload your python program. (You don't need to do this with CGI---it reloads Python and your program every time you serve another request. That's why it's so slow...)
 +
 
 +
To reload your program on Dreamhost with passenger:
 +
 
 
     $ touch tmp/restart.txt        # reload your program
 
     $ touch tmp/restart.txt        # reload your program
  
Using Passengers on Dreamhost:
+
To reload your program with mod_wsgi:
 +
 
 +
    $ touch wsgi_app.py
 +
 
 +
===Using Passengers on Dreamhost:===
 +
* https://www.byteconsole.com/get-flask-up-and-running-on-a-dreamhost-server-with-passenger/
 
* https://help.dreamhost.com/hc/en-us/articles/215769578-Passenger-overview
 
* https://help.dreamhost.com/hc/en-us/articles/215769578-Passenger-overview
 
* https://help.dreamhost.com/hc/en-us/articles/216137717-Python-overview
 
* https://help.dreamhost.com/hc/en-us/articles/216137717-Python-overview
Line 116: Line 122:
 
* https://help.dreamhost.com/hc/en-us/articles/215489338-Installing-virtualenv-and-custom-modules-in-Python
 
* https://help.dreamhost.com/hc/en-us/articles/215489338-Installing-virtualenv-and-custom-modules-in-Python
 
* https://help.dreamhost.com/hc/en-us/articles/115000221112-Using-pip-to-install-Python-modules
 
* https://help.dreamhost.com/hc/en-us/articles/115000221112-Using-pip-to-install-Python-modules
 +
 +
===Good Tutorials===
 +
Writing a full application with Apache, Bottle and MongoDB:
 +
* https://reachtim.com/articles/BAM-Short-Stack.html
  
 
==Python Installation==
 
==Python Installation==

Latest revision as of 06:13, 9 October 2019

Learning Python

I've reviewed these resources and highly recommend them.

If you have never programmed with Python before, you may want to start with DataCamp's Learn python by example:

If you want to just read, I recommend starting with the Python Tutorial:

If you have not programmed before, you may try:

If you have programmed before, at this point I recommend:

Using Python

Recommended packages and tools for different tasks

Developer Tools

  • PyCharm
  • Jupyter Notebook

Numerical Data Processing

  • numpy - numeric analysis in python (needed for matplotlib)
  • pandas or SparkSQL
  • tabulate - create tables easily (but see my tytable replacement)

Date Data

String Formatting

Image Manipulation

  • Use Pillow, the Python2/3 fork of Python Image Library (PIL).

Fun examples:

Text Processing

See Text processing notes.

Graphing

Easy Graphics

Games

Database Access

Python contains built-in support for SQLite3.

For using MySQL, you'll need a connector. There are many available.

Install MySQL connector on anaconda:

   conda install mysql-connector-python

Then you can:

   import mysql.connector as mysql
   c = mysql.connect(host=host,database=db,user=user,password=password)

I generally prefer PyMySQL, as it's pure-python and has fewer dependencies.

Python GUI Options

I need to write some programs that use GUI.

  • pyqt - Use PyQT5 (it's part of Anaconda)
  • FLTK, the Fast Light Toolkit

Building Web Applications

You basically have two options:

  • Write a CGI script (slow, but easy. Don't use for more than 1 request every 10 seconds)
  • Some sort of Web Server Gateway Interface (WSGI). Options include:
    • Bottle (easy, and the whole thing runs from a single file)
    • Flask (can run on Apache with Passenger, but needs its own domain name)
    • Writing your own (don't do this.)

The WSGI server needs to be run from a program that receives the Port80 connections. The two ways I've done this are:

  • mod_wsgi running within Apache. (This works surprisingly well and allows you to run bottle or flask side-by-side other systems)
  • CherryPy (Includes its own web server and will hand off to Bottle or Flas.)

For more information, see:

If you are using anything other CGI, you need some way to tell the web server to reload your python program. (You don't need to do this with CGI---it reloads Python and your program every time you serve another request. That's why it's so slow...)

To reload your program on Dreamhost with passenger:

   $ touch tmp/restart.txt         # reload your program

To reload your program with mod_wsgi:

   $ touch wsgi_app.py 

Using Passengers on Dreamhost:

Good Tutorials

Writing a full application with Apache, Bottle and MongoDB:

Python Installation

If you are using a Mac, you almost certainly want to ditch Apple's python implementation and instead install your own.

I now recommend the Anaconda Python distribution, because it has almost everything that you need, and it's easy to add new modules.

If you want to write a Python program with a GUI that can be run on any system, you'll need to use Tkinter (Python's Tcl/Tk bindings). If you want to actually enjoy what you are writing, you want to use PyQT5..

Advanced Python