Home Download Buy Blog Forum Support

how to view in browser

how to view in browser

Postby currybill on Wed Nov 23, 2011 11:29 am

Hello, I'm just starting to learn writing HTML & CSS. But I cannot find a way of viewing my work in a browser.
I downloaded the trial version to Windows Vista which is using I.E.9 as its default browser.
(I also have Safari and Chrome available)
Assistance much appreciated.
currybill
 
Posts: 2
Joined: Wed Nov 23, 2011 11:01 am

Re: how to view in browser

Postby C0D312 on Wed Nov 23, 2011 12:30 pm

To to Tools > New Plugin. Copy and paste this in the the plugin:
Code: Select all
import sublime, sublime_plugin
import webbrowser


class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      url = self.view.file_name()
      webbrowser.open_new(url)


Save this in your User folder of Sublime's packages.

Open up your User Keybindings. (Tools > Command Palette > "User Key bindings")
Add this somewhere to the list:
Code: Select all
   { "keys": ["ctrl+shift+b"], "command": "open_browser" }


You can make the "keys" anything you want. Now when you're in a HTML file, just press that command.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: how to view in browser

Postby currybill on Fri Nov 25, 2011 12:50 pm

Thanks, I've carried out your instruction and it still doesn't work. I would expect viewing in browser command an essential item in a text editor. Looks like I might have to go back to Textpad. I'm still not getting the full tool bar that shows on top of the ST2 window in the PSD Tuts tutorial. I wonder if this is specifically a Windows problem?
currybill
 
Posts: 2
Joined: Wed Nov 23, 2011 11:01 am

Re: how to view in browser

Postby C0D312 on Fri Nov 25, 2011 3:02 pm

currybill wrote: I'm still not getting the full tool bar that shows on top of the ST2 window in the PSD Tuts tutorial.


Do you mean Net Tuts? Because the "full tool bar" is called the command palette. To open it, press "ctrl+shift+p." They type in what you want. To test if the plugin worked, though, do you mind opening ST2 and loading a HTML file. Then open the python terminal by pressing (control+`). Something should pop up at the bottom of the screen. Type or copy and paste the following:
Code: Select all
view.run_command("open_browser")
. The html file should be opened in the browser. Let me know if it worked. If it did, you just need to fix your keybinding.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: how to view in browser

Postby capnhud on Fri Nov 25, 2011 8:42 pm

C0D312 wrote:To to Tools > New Plugin. Copy and paste this in the the plugin:
Code: Select all
import sublime, sublime_plugin
import webbrowser


class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      url = self.view.file_name()
      webbrowser.open_new(url)


Save this in your User folder of Sublime's packages.

Open up your User Keybindings. (Tools > Command Palette > "User Key bindings")
Add this somewhere to the list:
Code: Select all
   { "keys": ["ctrl+shift+b"], "command": "open_browser" }


You can make the "keys" anything you want. Now when you're in a HTML file, just press that command.


is possible to make this open in a particular browser?
capnhud
 
Posts: 62
Joined: Thu Jan 14, 2010 11:57 am

Re: how to view in browser

Postby C0D312 on Fri Nov 25, 2011 9:06 pm

The webbrowser module opens in the default browser. However, you can change the plugin to:
Code: Select all
import sublime, sublime_plugin
import webbrowser


class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      url = self.view.file_name()
      firefox = webbrowser.get('mozilla')
      firefox.open_new(url)


to always open in firefox. Thats about it...
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: how to view in browser

Postby jasondavis on Thu Dec 15, 2011 11:34 am

Hi I am trying to do something very similar, I am trying to make a plugin that will open a file in the browser just like the code you posted here except I need to do 2 things differently first.

1) I need to make it open ANY file type in the browser

2) I need to change the path before it is sent to the browser...

I need to replace
Code: Select all
E:\Server\htdocs\
in the file path with
Code: Select all
http://localhost/
and then open the corrected path in the browser

I would appreciate any help
jasondavis
 
Posts: 22
Joined: Thu Sep 01, 2011 12:52 am

Re: how to view in browser

Postby gezeichnet on Fri May 04, 2012 10:39 am

not the best solution (im not familiar with python and regex (have to learn it ;)) but here is my solution for opening in browser, if executed from the "htdocs" directory it runs in browser with "http://localhost/, not the best but it works

Code: Select all
import sublime, sublime_plugin
import webbrowser

class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      url = self.view.file_name()
      print url
      url = url.replace('\\', '/')
      # replace with your own paths
      url = url.replace('D:/xampp-win32-1.7.4-VC6/xampp/htdocs', 'http://localhost')
      webbrowser.open_new(url)


if anybody have a better solution i would love to see it
gezeichnet
 
Posts: 1
Joined: Fri May 04, 2012 10:32 am

Re: how to view in browser

Postby CanarySPA on Sun May 06, 2012 3:42 am

Hello!
Thanks for the little script, C0D312: D

It has helped, I even extended a little more to make it more customizable. I do not know Python but I needed to open the files in different browsers and not just one, I began to investigate ...

Here is the script somewhat improved:

To to Tools > New Plugin. Copy and paste this in the the plugin:

Code: Select all
import sublime, sublime_plugin
import webbrowser


class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit,keyPressed):
      url = self.view.file_name()
      if keyPressed == "1":
         navegator = webbrowser.get("open -a /Applications/Google\ Chrome.app %s")
      if keyPressed == "2":
         navegator = webbrowser.get("open -a /Applications/Firefox.app %s")
      if keyPressed == "3":
         navegator = webbrowser.get("open -a /Applications/Safari.app %s")
      navegator.open_new(url)



Save this in your User folder of Sublime's packages.

Open up your User Keybindings. (Tools > Command Palette > "User Key bindings")
Add this somewhere to the list:

Code: Select all
{ "keys": ["alt+1"], "command": "open_browser", "args": {"keyPressed": "1"}},
   { "keys": ["alt+2"], "command": "open_browser", "args": {"keyPressed": "2"}},
   { "keys": ["alt+3"], "command": "open_browser", "args": {"keyPressed": "3"}}



You can add more or remove browsers. You just have to add or modify the browser path to the key that is pressed and ready!

I hope you will help. Sorry for my English, I'm Spanish ...

P.D: The paths to the browsers in the example are for the Mac version.

Greetings! : D
CanarySPA
 
Posts: 2
Joined: Sun May 06, 2012 3:34 am

Re: how to view in browser

Postby 718bklynqns on Sun May 27, 2012 4:58 pm

Hey CanarySpa,

Thanks for your script, pretty impressive for someone who doesn't know Python!
Your script works, but in each case Safari, Chrome, Firefox etc. my file either shows up as text in the browser or just downloads...

I am using this method http://www.sublimetext.com/forum/viewtopic.php?f=3&t=6232&p=29879&hilit=localhost#p29879

But it falls short cause you only get one default browser to preview, and you can't set another browser with it...

Is there any conflict going on because of MAMP PRO??

Thanks so much!!!
718bklynqns
 
Posts: 4
Joined: Sat May 26, 2012 12:55 pm
Location: queens

Next

Return to General Discussion

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 16 guests

cron