0

I'm trying to set up a financial chart in Conda Pycharm IDE using Plotly Express, but for some reason I'm not able to run the script in Pycharm since the graph keeps opening in my chrome browser instead. I have already installed conda in the terminal, but it still doesn't open the chart internally in the IDE. The chart is only extracting 'date' and 'close' values from the API Key. This is the data (you can just web scrape the latest 1000 data points from yahoo finance and you will get the exact same numbers):

FB Stock Data

The code is as follows:

import requests #for http request to Marketstack.com
import pandas as pd
import numpy as np

# Api Key
params = {
    'access_key': '*************************'
}

# Request Api Key Data
api_result = requests.get('https://api.marketstack.com/v1/eod?access_key=**********************&symbols=FB&sort=DESC&limit=1000', params)
api_response = api_result.json()

# Sorts the data into a table
df = pd.DataFrame(api_response['data'])
print(df)

# Exports and then imports csv data
df.to_csv('Test_Sample.csv', index=False)
dataframe = pd.read_csv('Test_Sample.csv', header=0)

# Reverse data table
dataframe2 = dataframe.iloc[::-1]
print(dataframe2)

# Convert string to floats
dataframe2['symbol']=dataframe2['symbol'].astype(str)
dataframe2['exchange']=dataframe2['exchange'].astype(str)
dataframe2['date']=dataframe2['date'].astype(str)
#dataframe2['symbol']=pd.to_numeric(dataframe2['symbol'], errors='coerce')
#dataframe2['exchange']=pd.to_numeric(dataframe2['exchange'], errors='coerce')
#dataframe2['date']=pd.to_numeric(dataframe2['date'], errors='coerce')

# Change X-axis to a series of integers
dataframe2.index=pd.to_numeric(np.arange(len(dataframe2)))
print(dataframe2)

# Convert date string to datetime index:
dataframe2.index = pd.DatetimeIndex(df['date'])
data=dataframe2
#data = dataframe2.iloc[::-1]
#aapl_df.index = pd.DatetimeIndex(df['date'].values) #Alternative pseudocode

import plotly.express as px
#import plotly.io as pio
#pio.renderers.default= 'svg'
#gapminder = px.data.gapminder()
fig = px.line(data, x='date', y='close', title='Financial Instrument')

for template in["plotly_dark"]:
    fig.update_xaxes(
        rangeslider_visible=True,
        rangeselector=dict(
            buttons=list([
                dict(count=1, label="1m", step="month", stepmode="backward"),
                dict(count=6, label="6m", step="month", stepmode="backward"),
                dict(count=1, label="YTD", step="year", stepmode="todate"),
                dict(count=1, label="1y", step="year", stepmode="backward"),
                dict(step="all")
            ])
        )
    )
    fig.show()
    #fig.show(renderer="svg")
  • Not everyone has an API_key. Keeping that in mind, could you please provide sample data in text format? That is the quickest approach to get the fastest answer. – r-beginners Apr 26 '21 at 13:59
  • Done :) You can also just use data from yahoo finance and you will get the exact same input. – NinjaCoder98 Apr 26 '21 at 14:30
  • "I'm not able to run the script in Pycharm since the graph keeps opening in my chrome browser instead" - so the script _actually runs_, but shows the plot in the browser, not PyCharm, right? Here are some similar questions: https://stackoverflow.com/questions/50250010/plotly-chart-is-not-displayed-in-pycharm https://stackoverflow.com/questions/57211392/how-can-i-see-plotly-graphs-in-pycharm. Also, where exactly in PyCharm do you want the plot to be shown? Why not simply use a Jupyter notebook? – ForceBru Apr 26 '21 at 14:38
  • Please present the data in text, not images. That will get you the most answers. – r-beginners Apr 26 '21 at 14:43
  • @ForceBru yes, it is trying to run in chrome, but ends up just loading and loading. I'm trying to use conda since it is what I'm most familiar with and it's something I prefer, that's really the only reason. Should I us Jupyter notebook as the backend or is Conda better once it's working? – NinjaCoder98 Apr 26 '21 at 16:24
  • @r-beginners shared it as a link – NinjaCoder98 Apr 26 '21 at 16:30
  • @NinjaCoder98, I don't think conda has anything to do with this. In fact, you can install Jupyter with conda. Plotly definitely works well with Jupyter, so you should probably just use that – ForceBru Apr 26 '21 at 17:28
  • @ForceBru ok I'll try that then and see how it goes. Thank you! – NinjaCoder98 Apr 26 '21 at 19:03

0 Answers0