IslandT: Use the Requests module to directly retrieve the market data

Hello and welcome back to our cryptocurrency project. In the previous article I had mentioned before that I want to use the cryptocompy module to create our new cryptocurrency project, however, after a closer look at the CriptoCompare API I think we have better used the original API to make the rest call instead of using the wrapper module because the original API seems to provide more returned data type than the one offered by the cryptocompy module.

Before we go even further I just want to make two corrections here:

  1. It seems like we do not need the API key from CriptoCompare when we make the API call which is kind of confusing.
  2. The data from both calls are the same which means cryptocompy module is indeed a wrapper of the CriptoCompare API. Previously I thought that data s not the daily data directly from the original CriptoCompare site but instead a weekly data.

Below are the two calls using both the wrapper and the original API (directly using the Requests module) and it seems like the returning data for both of them are the same.

 import pandas as pd from cryptocompy import price import requests  # cryptocompy module coin_data = price.get_current_price("BTC", ["EUR", "JPY", "USD"]) print(pd.DataFrame.from_records(coin_data))  print("")  # direct API call r = requests.get("https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD,JPY,EUR") print(r.json()) 
Both calls returned the same data

In the next chapter, we will first use tkinter to create the user interface for our application, then we will write various API calls using the requests module to get different data set and show those outcomes on the display screen of our cryptocurrency application.

Planet Python

PSF GSoC students blogs: Week-8: Remodeling the data access Mechanism

Hello folks,

This week I worked as a package mechanic, developing two mechanisms for our package wsynphot. 😜 Firstly I migrated the mechanism used by our package to access filter data from a HDF storage to the cache on disk. Then I started setting up an update mechanism for the cached filter data to keep it up-to-date.  Let me share how!

What did I do this week?

  1. I changed the data access methods in base module of our package to use filter data cache (which is handled by the module I created last week), instead of using HDF data file. This seemingly simple task, was successfully completed after doing a couple of unanticipated tasks:
    • Fixed a problem in base module to access a calibration file. I dug rabbit holes to find the cause of this annoying problem – thanks to the search history option of git and the discussions I had with my mentor, that I finally solved it.
    • Fixed the all-of-sudden failing IO tests due to a recent update in filter data at SVO.
    • Integrated the functions from cache_filters module into the base module and made sure it works fine.
    • Removed the older code that was meant for handling HDF storage of filter data, along with some other necessary cleanup in the package.
    • Re-documented quickstart notebook due to these changes.
  2. Next I started working on cache updating mechanism to ensure that filter data on user’s disk is up-to-date with that at SVO (our data source).
    • For this I came up with a logic to compare filter index in cache with that at SVO online and then calculate the filters to add/remove in cache by using set difference operations. I felt great about it realizing how set theory helps in solving practical problems! 😌
    • I created a function for implementing the same and solved several problems I encountered in the process, like data type conflicts (byte vs unicode), etc.
    • I tested the function against a recent update at SVO, but I noticed discrepancies in the result which I traced back to find that they have updated their web interface but not their API which we’re using. So I mailed their team for informing them about it & they instantly fixed it.

 

What is coming up next?

Now I need to finish up my work on developing this update cache mechanism. Also we have planned to mend some documentation of our package to make it accessible for more users.

 

Did I get stuck anywhere?

Not as such, all the seemingly difficult problems that I encountered, eventually got solved.

 

What was something new I learned?

🗃️ Accessing package data: The data we store in our package doesn’t get included while building it unless we specify it in package_data which is often configurable from setup.py file. This needs us to specify path of files we want to include in built package by using file globbing patterns. So I also learned that file globbing doesn’t work recursively if we use /*, so we need to make sure to specify the paths of all depths we want to be included.

🔎 Tracing how a piece of code changed throughout the history of a project: Thanks to git that it provides numerous options with log command, using which you can find all the commits that affected a string (let it be the function name, or any other uniquely-identifying part of the code you want to search). It came into my use when I needed to know why the function listed in a error message of our package, doesn’t exist in the codebase.

📝 How to edit and install a package simultaneously: Earlier I used to edit code files of the installed package in /../site-packages/ (which is even referred as bad practice) but I couldn’t track the changes I made because it is not in a git repo. So I looked for a better approach on internet and discussed with my mentor to find that I can use setup.py develop instead of installing the package. This creates a .egg-link in the site-packages back to the project source code directory so that you can see the changes directly without having to reinstall every time you made an edit in package.

 


Thank you for reading. Stay tuned to learn more things with me!

Planet Python

Teilen Sie Data Insights mit Ihren Kunden: Ein Überblick über Embedded Analytics

Stellen Sie externen Kunden Dashboards, Berichte oder andere Daten zur Verfügung?

Falls ja, dann wissen Sie, dass die Nutzung davon abhängt, ein leicht zugängliches, navigierbares und verständliches Erlebnis anzubieten. Durch die Integration von Tableau-Visualisierungen in Ihre Systeme und Workflows können Sie diese Erlebnisse noch einfacher und leistungsfähiger bereitstellen.

Download Now

  • I understand that InterWorks will use the data provided for the purpose of communication and the administration of my request. InterWorks will never disclose or sell any personal data except where required to do so by law. Finally, I understand that future communications related topics and events may be sent from InterWorks, but I can opt-out at any time.
  • This field is for validation purposes and should be left unchanged.

The post Teilen Sie Data Insights mit Ihren Kunden: Ein Überblick über Embedded Analytics appeared first on InterWorks.

InterWorks

Stack Abuse: Python’s Bokeh Library for Interactive Data Visualization

Introduction

In this tutorial, we’re going to learn how to use Bokeh library in Python. Most of you would have heard of matplotlib, numpy, seaborn, etc. as they are very popular python libraries for graphics and visualizations. What distinguishes Bokeh from these libraries is that it allows dynamic visualization, which is supported by modern browsers (because it renders graphics using JS and HTML), and hence can be used for web applications with a very high level of interactivity.

Bokeh is available in R and Scala language as well; however, its Python counterpart is more commonly used than others.

Installation

The easiest way to install Boken using Python is through pip package manager. If you have pip installed in your system, run the following command to download and install Bokeh:

$   pip install bokeh 

Note: If you choose this method of installation, you need to have numpy installed in your system already

Another method to install Bokeh is through Anaconda distribution. Simply go to your terminal or command prompt and run this command:

$   conda install bokeh 

After completing this step, run the following command to ensure that your installation was successful:

$   bokeh --version 

If the above command runs successfully i.e. the version gets printed, then you can go ahead and use bokeh library in your programs.

Coding Exercises

In this part, we will be doing some hands-on examples by calling Bokeh library’s functions to create interactive visualizations. Let’s start by trying to make a square.

Note: Comments in the codes throughout this article are very important; they will not only explain the code but also convey other meaningful information. Furthermore, there might be ‘alternative’ or additional functionality that would be commented out, but you can try running it by uncommenting those lines.

Plotting Basic Shapes

Here we specify the x and y coordinates for points, which will be followed in sequence when the line is being drawn. The figure function instantiates a figure object, which stores the configurations of the graph you wish to plot. Here we can specify both the X range and Y range of the graph, which we set from 0 to 4, which covers the range of our data. The line method then draws a line between our coordinates, which is in the shape of a square.

from bokeh.io import output_file, output_notebook   from bokeh.plotting import figure, show  x = [1, 3, 3, 1, 1]   y = [1, 1, 3, 3, 1]  # Display the output in a separate HTML file  output_file('Square.html', title='Square in Bokeh')   #output_notebook() # Uncomment this line to use iPython notebook  square = figure(title='Square Shape',                plot_height=300, plot_width=300,              x_range=(0, 4), y_range=(0, 4))  # Draw a line using our data square.line(x, y)   #square.circle(x, y) # Uncomment this line to add a circle mark on each coordinate  # Show plot show(square)   

You may have noticed in the code that there is an alternative to the output_file function, which would instead show the result in a Jupyter notebook by using the output_notebook function. If you’d prefer to use a notebook then replace the output_file function with output_notebook in the code throughout this article.

When you run the above script, you should see the following square opening in a new tab of your default browser.

Output:

square plot

In the image above, you can see the tools on the right side (pan, box zoom, wheel zoom, save, reset, help – from top to bottom); these tools enable you to interact with the graph.

Another important thing which will come in handy is that after every call to the “show” function if you create a new “figure” object, a subsequent call to the “show” function with the new figure passed as an argument would generate an error. To resolve that error, run the following code:

from bokeh.plotting import reset_output  reset_output()   

The reset_output method resets the figure ID that the show function currently holds so that a new one can be assigned to it.

What we’ve done so far is rather basic, let’s now try to make multiple lines/map equations in a single graph. The most basic example for that would be to try and draw lines for the equations y = x, y = x^2, and y = x^3. So let’s see how we can make a graph to display them all at once using Bokeh:

from bokeh.plotting import figure, output_file, show  # Declare data for our three lines x = [1, 2, 3, 4, 5, 6]   x_square = [i**2 for i in x]   x_cube = [i**3 for i in x]  # Declare HTML file as output for when show is called output_file("Eqs.html")  lines = figure(title='Line Comparisons', x_range=(0, 8), y_range=(0,100),      x_axis_label='X-Axis', y_axis_label='Y-Axis')   lines.line(x, x, legend="y = x", line_width=3) # Line for the equation y=x   lines.square(x, x, legend="y = x", size=10) # Add square boxes on each point on the line  lines.line(x, x_square, legend="y = x^2", line_width=3) #Line for the equation y=x^2   lines.circle(x, x_square, legend="y = x^2", size=10) # Add circles to points since it partially overlaps with y=x  lines.line(x, x_cube, legend="y = x^3", line_width=3) # Line for the equation y=x^3   lines.square(x, x_cube, legend="y = x^2", size=10) # Add square boxes on each point of the line  # Display the graph show(lines)   

Output:

line comparisons graph

Before we continue to plot a few more graphics, let’s first learn a few cool tricks to make your graphics more interactive, as well as aesthetic. For that we’ll first of all learn about the different tools that the Bokeh Library uses apart from the ones that are displayed alongside (either on top or on the right side) the graph. The explanations will be provided in the comments of the code below:

# Use the same plot data as above x = [1, 2, 3, 4, 5, 6]   x_square = [i**2 for i in x]   x_cube = [i**3 for i in x]  #now let's make the necessary imports. Note that, in addition to the imports we made in the previous code, we'll be importing a few other things as well, which will be used to add more options in the 'toolset'.   # Same imports as before from bokeh.plotting import figure, output_file, show  # New imports to add more interactivity in our figures # Check out Bokeh's documentation for more tools (these are just two examples) from bokeh.models import HoverTool, BoxSelectTool  output_file("Eqs.html")  # Add the tools to this list tool_list = [HoverTool(), BoxSelectTool()]  # Use the tools keyword arg, otherwise the same lines = figure(title='Line Comparisons', x_range=(0, 8), y_range=(0, 100),      x_axis_label='X-Axis', y_axis_label='Y-Axis', tools=tool_list)  # The rest of the code below is the same as above lines.line(x, x, legend="y = x", line_width=3)   lines.square(x, x, legend="y = x", size=10)  lines.line(x, x_square, legend="y = x^2", line_width=3)   lines.circle(x, x_square, legend="y = x^2", size=10)  lines.line(x, x_cube, legend="y = x^3", line_width=3)   lines.square(x, x_cube, legend="y = x^2", size=10)  # Display the graph show(lines)   

Output:

extra tools

In the above picture, you can see the two extra options added to the previously available tools. You can now also hover over any data point and its details will be shown, and you can also select a certain group of data points to highlight them.

Handling Categorical Data with Bokeh

Next thing that we’ll learn to do using Bokeh library is handling categorical data. For that, we’ll try and make a bar chart first. To make it interesting, let’s try and create a chart which represents the number of world cups won by Argentina, Brazil, Spain, and Portugal. Sounds interesting? Let’s code it.

from bokeh.io import show, output_file   from bokeh. plotting import figure  output_file("cups.html")  # List of teams to be included in the chart. Add or # remove teams (and their World Cups won below) to # see how it affects the chart teams = ['Argentina', 'Brazil', 'Spain', 'Portugal']  # Activity: We experimented with the Hover Tool and the # Box Select tool in the previous example, try to # include those tools in this graph  # Number of world cups that the team has won wc_won = [5, 3, 4, 2]  # Setting toolbar_location=None and tools="" essentially # hides the toolbar from the graph barchart = figure(x_range=teams, plot_height=250, title="WC Counts",              toolbar_location=None, tools="")  barchart.vbar(x=teams, top=wc_won, width=0.5)  # Acitivity: Play with the width variable and see what # happens. In particular, try to set a value above 1 for # it   barchart.xgrid.grid_line_color = 'red'   barchart.y_range.start = 0  show(barchart)   

Output:

World cup count graph

Do you notice something in the graph above? It’s quite simple, and unimpressive, no? Let’s make some changes in the above code, and make it a bit more colorful and aesthetic. Bokeh has a lot of options to help us with that. Let’s see what we can do with it:

# Mostly the same code as above, except with a few # additions to add more color to our currently dry graph  from bokeh.io import show, output_file   from bokeh.plotting import figure  # New imports below from bokeh.models import ColumnDataSource  # A was added 4 to the end of Spectral because we have 4 # teams. If you had more or less you would have used that # number instead from bokeh.palettes import Spectral4  from bokeh.transform import factor_cmap  output_file("cups.html")  teams = ['Argentina', 'Brazil', 'Spain', 'Portugal']   wc_won = [5, 3, 4, 2]  source = ColumnDataSource(data=dict(teams=teams, wc_won=wc_won, color=Spectral4))  barchart = figure(x_range=teams, y_range=(0,8), plot_height=250, title="World Cups Won",              toolbar_location=None, tools="")  barchart.vbar(x='teams', top='wc_won', width=0.5, color='color', legend='teams', source=source)   # Here we change the position of the legend in the graph # Normally it is displayed as a vertical list on the top # right. These settings change that to a horizontal list # instead, and display it at the top center of the graph barchart.legend.orientation = "horizontal"   barchart.legend.location = "top_center"  show(barchart)   

Output:

improved World Cup count graph

Evidently, the new graph looks a lot better than before, with added interactivity.

Before concluding this article, I’d like to let you all know that this was just a glimpse of the functionality that Bokeh offers. There are tons of other cool things that you can do with it, and you should try them out by referring to Bokeh’s documentation and following the available examples.

Conclusion

To sum it up, in this tutorial we learned about the Bokeh library’s Python variant. We saw how to download and install it using the pip or anaconda distribution. We used Bokeh library programs to make interactive and dynamic visualizations of different types and using different data types as well. We also learned, by seeing practical examples, the reason why Bokeh is needed even though there are other more popular visualization libraries like matplotlib and Seaborn available. In short, Bokeh is very resourceful and can pretty much do all kinds of interactive visualizations that you may want.

Planet Python