Boston Python Workshop/Friday handout/Windows

From OpenHatch wiki

About Django on Windows

  • These instructions should work for all versions of Windows from XP on to Windows 7.
  • You may need to login as Administrator, or give the Administrator password when installing some programs, depending on your Windows version and user settings.

Open a Command Prompt

  • Open a command prompt window, and keep it open along with your browser. Much of installing Python and using Django is typing commands and hitting <enter>. Your experience using Rails on Windows greatly depends on your making friends with the command prompt window.
  • This is also called the command prompt, command window, command-line window, MS-DOS or DOS window.

http://www.wiki.devchix.com/index.php?title=Opening_a_command_prompt_window

  • Try these recommendations:

http://www.wiki.devchix.com/index.php?title=Recommended_setup_for_command-line_windows

  • Tip: clear screen
    If you ever want to clear the "output history" to get a clear screen, type:
cls
  • Tip: command history
    The command prompt window stores a "command history." To view and re-run previous commands, use the <up arrow> and <down arrow> keys. You can also edit a previous command and run it--this is handy for long commands, or fixing mistakes.
  • Tip: copy and paste
    In the instructions below, where it says: "In the command prompt type:", you can, much more easily, copy the command from this page, and right click in the menu bar or command prompt window, then click on "Paste", then hit the <enter> key.

Install Python

  • Test your Python install in the command prompt using the "-v" version flag:
\python27\python.exe -v

You should see something like

Python 2.7.1 (r271:86832, ...) on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
  • Type
exit()

and press Enter, and you are back to the command prompt.

Install Git for Windows

  • Go to http://code.google.com/p/msysgit/ (aka GitBash)
    • Click on the top "Downloads" tab
    • Download the top .exe file link with the Summary something like "Full installer for official Git 1.7.x.x"
  • Run the install.
  • In the "Select Components" dialog, make sure the checkboxes "Associate ... configuration files ..." and "Use a TrueType font ..." are checked.
  • Important: In the "Adjusting your PATH environment" dialog, make sure "Run Git from the Windows Command Prompt" is selected.
  • In the "Configuring the line ending conversions" dialog, make sure "Checkout Windows-style, commit Unix-style line endings" is selected.
  • To run git, open Programs > Git > Git Bash
  • Then to test the Git install and see Git commands, in the command prompt type:
git

Configure Git

  • Set up Git with your name and email to tag your code changes. In the command prompt type:
 git config --global user.name <Your ActualName> 

and:

 git config --global user.email <YourEmail@domain.com> 
  • Tip: Colors


To have colored output, in the command prompt type each line followed by <enter>:

 
git config --global color.diff auto 
git config --global color.status auto 
git config --global color.branch auto 

Install SQLite Manager

  • If you already have Firefox installed, verify that it is version 3.5.0 or greater. (Help -> About Mozilla Firefox. The version number is right under the "Firefox" title.)
  • If you don't have Firefox installed, or it's an older version, install Firefox.
  • Once it's installed, open Firefox and go to Tools -> Add-ons. At the top of the add-ons window, click "Get Add-ons."
  • There will be search box directly underneath "Get Add-ons" that says "Search All Add-ons." Enter "SQLite" (without the quotes) in the box and hit enter.
  • SQLite Manager should be the top result. Click "Add to Firefox..." (If SQLite Manager isn't in the results, check the spelling - SQLite only has one L. Also, check that you have at least Firefox 3.5.)
  • Wait for the countdown, then click "Install Now."
  • In the Add-ons windows, click "Restart Firefox."
  • Once Firefox restarts, the Add-ons window should say "1 new add-on has been installed." Go to the Tools menu and verify that there is an option for SQLite Manager.


Install KomodoEdit

You need a text editor to do Python. If you already have a preferred text editor, such as vi, emacs, jedit, etc., you can skip this step. It must be a plain text editor and not something with styling like Microsoft Word.

When in doubt, use KomodoEdit.

Create an ssh public key

You'll need one of these to create your Heroku account in the next section.

  • Open up a new GitBash window.
  • ssh-keygen -C "Your Actual Email" -t rsa (email should match git config setting)
  • Hit enter to accept default location for ssh key.
  • Hit enter to accept blank passphrase (if computer is shared with other people, as in a work laptop, you should create a passphrase). Hit enter again to accept blank passphrase (or enter passphrase again).
  • Output of ssh-keygen command
  • Your brand-new public key is now stored at ~/.ssh/id_rsa.pub.

Verify you can create a new Rails app

  • Open a new GitBash window and type the following with a return at the end of each line:
   mkdir ~/Desktop/rails
   cd ~/Desktop/rails 
   rails new test_app
  • The first two commands should produce no output. The last command's output is voluminous. :)
  • Once that's finished, type the following in the GitBash window with a return at the end of each line:
   cd test_app
   rails server
  • The first command should produce no output. The second command will put out a bunch of output, then just sit there until you cancel it (2 steps from now).
  • In your browser, go to http://localhost:3000 (screenshot of successful install
  • Back in the GitBash window where you ran rails server, type control-c to kill(stop) the server.

Verify your database is set up

  • Open a new GitBash window.
cd ~/Desktop/rails/test_app
rails generate scaffold user name:string email:string address:text active:boolean
rake db:migrate
rails server

In the browser, visit http://localhost:3000/users click New user to create a user to make sure we can save to the database. (The window where you ran rails server will display debugging information as you do so.) In your GitBash window where you ran rails server, type control-c to kill(stop) the server.

Verify git is working

  • Open a new GitBash window.
  • Type the following commands:
cd ~/Desktop/rails/test_app 
git init

This should generate output similar to:

 Initialized empty Git repository in c:/Documents and Settings/smei/Desktop/ruby_on_rails/test_app/.git/
  • Type the following commands:
    • git add . (Note the dot) (May get line ending warnings; safe to ignore.)
    • git commit -m "initial commit" (Output of initial check-in)
    • git log (We're just checking to make sure it worked. Verify that it has the right user and commit message.)

Verify Heroku is set up

  • Run your Text Editor
    • use file-open to select Desktop/rails/test_app/Gemfile
    • replace the sqlite3 entry in your Gemfile with this chunk of code (the first line is what was there originally, with # space put in front to comment it out).
  # gem 'sqlite3-ruby', :require => 'sqlite3'
  group :production, :staging do
    gem "pg", "0.9.0"
  end
  
  group :development, :test do
    gem "sqlite3-ruby", :require => "sqlite3"
  end

(Don't delete the rest of the gems, just replace the sqlite gem entry.)

  • Open a new GitBash window and type the following commands:
cd ~/Desktop/rails/test_app
bundle update

this rebuilds the Gemfile.lock file to reflect the change.

git add .
git commit -m "fixed the gemfile"

Also, as of now heroku requires you to specify a special stack if running rails3 so your create command needs to be like this:

heroku create --stack bamboo-mri-1.9.2
  • Enter your Heroku email address and password. (Output of heroku create command)
  • To verify that the heroku create completed successfully, do git remote show and see if the list includes heroku.
  • type:git push heroku master
  • It may ask: "The authenticity of host 'heroku.com (75.101.145.87)' can't be established. RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad. Are you sure you want to continue connecting (yes/no)?" Type yes and hit <enter>.
  • Output of successful first deploy. Be sure to find your Heroku application name in the output. (What to do if git seems stuck)
  • In the GitBash window type: heroku rake db:migrate (Output of heroku rake db:migrate)
  • Go to your application's URL. You'll need your Heroku application name. The URL for your app is application name.heroku.com - so with the example output in the previous step, it would be floating-winter-18.heroku.com. Verify you see the welcome page. Leave this window open.
  • In the browser, add /users to the end of the URL. Verify you see the user list page. Create a new user to verify you can write to the db on Heroku.
  • If you are having issues ("Remote end hung up unexpectedly"), you might need to run "heroku keys:add"

Congratulations!

You have everything you need to write a Ruby on Rails application.

Almost there!

Find a volunteer and have them watch the next steps. If this works - get a sticker from them for your computer!

After running this, the last few lines of output should look something like this: 11 scenarios (5 failed, 6 skipped) 84 steps (5 failed, 79 skipped) 0m0.998s rake aborted! Command failed with status (1): [bundle exec /Users/sasha/.rvm/rubies/ruby-...]

(See full trace by running task with --trace)

Cleanup

Ok, there is one more step. You won't be using the test application in the workshop; we just created it to make sure everything was working. You should delete it now to reduce confusion during the workshop. Don't worry about losing information - you'll repeat all the steps tomorrow, but more slowly so you can better understand what's going on under the hood.

  • Drag the test_app folder (inside rails) to the trash. Leave rails on the desktop.
  • Delete the app from Heroku. Go to https://heroku.com/myapps and then click on your app name. Scroll to the bottom of the page and click Destroy App.