Plots a figure in IPython, creates an HTML or generates an Image

	figure : figure
		Plotly figure to be charted
	validate : bool
		If True then all values are validated before 
		it is charted
	sharing : string
		Sets the sharing level permission
			public - anyone can see this chart
			private - only you can see this chart
			secret - only people with the link can see the chart
	filename : string
		Name to be used to save the file in the server, or as an image
	online : bool
		If True then the chart/image is rendered on the server 
		even when running in offline mode. 
	asImage : bool
			If True it returns an Image (png)
			In ONLINE mode:
				Image file is saved in the working directory				
					Accepts:
						filename
						dimensions
						scale
						display_image
			In OFFLINE mode:
				Image file is downloaded (downloads folder) and a 
				regular plotly chart is displayed in Jupyter
					Accepts:
						filename
						dimensions
	asUrl : bool
		If True the chart url/path is returned. No chart is displayed. 
			If Online : the URL is returned
			If Offline : the local path is returned
	asPlot : bool
		If True the chart opens in browser
	dimensions : tuple(int,int)
		Dimensions for image
			(width,height)		
	display_image : bool
		If true, then the image is displayed after it has been saved
		Requires Jupyter Notebook
		Only valid when asImage=True

	Other Kwargs
	============
		legend : bool
			If False then the legend will not be shown
		scale : integer
			Increase the resolution of the image by `scale` amount
			Only valid when asImage=True

def iplot(figure,validate=True,sharing=None,filename='',
			 online=None,asImage=False,asUrl=False,asPlot=False,
			 dimensions=None,display_image=True,**kwargs):
	"""
	Plots a figure in IPython, creates an HTML or generates an Image

	figure : figure
		Plotly figure to be charted
	validate : bool
		If True then all values are validated before 
		it is charted
	sharing : string
		Sets the sharing level permission
			public - anyone can see this chart
			private - only you can see this chart
			secret - only people with the link can see the chart
	filename : string
		Name to be used to save the file in the server, or as an image
	online : bool
		If True then the chart/image is rendered on the server 
		even when running in offline mode. 
	asImage : bool
			If True it returns an Image (png)
			In ONLINE mode:
				Image file is saved in the working directory				
					Accepts:
						filename
						dimensions
						scale
						display_image
			In OFFLINE mode:
				Image file is downloaded (downloads folder) and a 
				regular plotly chart is displayed in Jupyter
					Accepts:
						filename
						dimensions
	asUrl : bool
		If True the chart url/path is returned. No chart is displayed. 
			If Online : the URL is returned
			If Offline : the local path is returned
	asPlot : bool
		If True the chart opens in browser
	dimensions : tuple(int,int)
		Dimensions for image
			(width,height)		
	display_image : bool
		If true, then the image is displayed after it has been saved
		Requires Jupyter Notebook
		Only valid when asImage=True

	Other Kwargs
	============
		legend : bool
			If False then the legend will not be shown
		scale : integer
			Increase the resolution of the image by `scale` amount
			Only valid when asImage=True		
	"""
	valid_kwargs=['world_readable','legend','scale']

	for key in list(kwargs.keys()):
		if key not in valid_kwargs:
			raise Exception("Invalid keyword : '{0}'".format(key))
	
	if 'legend' in kwargs:
		if 'layout' in figure:
			figure['layout'].update(showlegend=kwargs['legend'])

	## Sharing Values
	if all(['world_readable' in kwargs,sharing is None]):
		sharing=kwargs['world_readable']
	if isinstance(sharing,bool):
			if sharing:
				sharing='public'
			else:
				sharing='private'
	if sharing is None:
		sharing=auth.get_config_file()['sharing']

	## Filename Handling
	if not filename:
		# if not figure.get('layout', None):
		# 	figure['layout'] = {}
		try:
			filename=figure['layout']['title']
		except:
			filename='Plotly Playground {0}'.format(time.strftime("%Y-%m-%d %H:%M:%S"))

	## Dimensions
	if not dimensions:
		dimensions=(800,500) if not auth.get_config_file()['dimensions'] else auth.get_config_file()['dimensions']

	## Offline Links
	show_link = auth.get_config_file()['offline_show_link']
	link_text = auth.get_config_file()['offline_link_text']
	config = auth.get_config_file()['offline_config']

	## Remove validation if shapes are present
	if 'layout' in figure:
		validate = False if 'shapes' in figure['layout'] else validate

	## asURL
	auto_open=True
	if asUrl:
		asPlot=True
		auto_open=False

	## Exports
	if asImage:
		if offline.is_offline() and not online:
			return offline.py_offline.iplot(figure,validate=validate, filename=filename, show_link=show_link,link_text=link_text,
				image='png', image_width=dimensions[0], image_height=dimensions[1], config=config)
		else:
			try:
				py.image.save_as(figure,filename='img/'+filename,format='png',
					width=dimensions[0],height=dimensions[1],scale=kwargs.get('scale',None))
				path='img/'+filename+'.png'
			except:
				py.image.save_as(figure,filename=filename,format='png',
					width=dimensions[0],height=dimensions[1],scale=kwargs.get('scale',None))
				path=filename+'.png'
			if display_image:
				return display(Image(path))
			else:
				print('Image saved : {0}'.format(path))
				return None

	## asPlot and asUrl
	if asPlot:
		filename+='.html'
		if offline.is_offline() and not online:
			return offline.py_offline.plot(figure, filename=filename, validate=validate,
								show_link=show_link, link_text=link_text, auto_open=auto_open, config=config)
		else:
			return py.plot(figure, sharing=sharing, filename=filename, validate=validate,
							auto_open=auto_open)

	## iplot
	if offline.is_offline() and not online:	
		return offline.py_offline.iplot(figure, validate=validate, filename=filename, show_link=show_link, link_text=link_text, config=config)
	else:		
		return py.iplot(figure,validate=validate,sharing=sharing,
						filename=filename)