eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have been attempting to create a application that contains two Text() widgets, both of which can dynamically resize when the window size is changed. Before I have always used the root.pack() manager, with fill='both' and expand=True. While this works for LabelFrames and most other widgets, it does not work when a Text widget is resized smaller then its original dimensions. Is there a way to have dynamically resizing Text widgets? Ex. import tkinter as tk window = tk.Tk() editor = tk.Text(bg='red') editor.pack(side='top',fill='both',expand=True) output = tk.Text(bg='green') output.pack(side='top',fill='both',expand=True) window.mainloop() 

Answer: Tkinter will try to honor the requested size of a text widget. Since you didn't specify a size, the text widget will request a size of 80x24. When you resize the window smaller, pack tries to make room for everything at its requested size, and it does so in stacking order. As the window shrinks, there's room for all of the first text widget but not enough for both. Because there's not enough room, it has to subtract space from the remaining widgets. Thus, it starts chopping off the last text widget. To combat this, you can set the requested size of the text widgets to a small value that will fit in almost any window size, and then force them to grow by setting the size of the window as a whole. This way, pack will first allocate enough space for each small window, and then expand them equally when there's extra space. For example: import tkinter as tk window = tk.Tk() window.geometry("400x400") editor = tk.Text(bg='red', width=1, height=1) output = tk.Text(bg='green', width=1, height=1) editor.pack(side='top',fill='both',expand=True) output.pack(side='top',fill='both',expand=True) window.mainloop()  The other solution is to use grid which lets you specify that rows and columns should be of uniform size. import tkinter as tk window = tk.Tk() editor = tk.Text(bg='red') output = tk.Text(bg='green') editor.grid(row=0, column=0, sticky="nsew") output.grid(row=1, column=0, sticky="nsew") window.grid_columnconfigure(0, weight=1) window.grid_rowconfigure((0,1), weight=1, uniform=1) window.mainloop()
Question: I have a plotly.express.scatter plot with thousands of points. I'd like to add text labels, but only for outliers (eg, far away from a trendline). How do I do this with plotly? I'm guessing I need to make a list of points I want labeled and then pass this somehow to plotly (update_layout?). I'm interested in a good way to do this. Any help appreciated.

Answer: You have the right idea: you'll want to have the coordinates of your outliers, and use Plotly's text annotations to add text labels to these points. I am not sure how you want to determine outliers, but the following is an example using the tips dataset. import pandas as pd from sklearn import linear_model import plotly.express as px df = px.data.tips() ## use linear model to determine outliers by residual X = df["total_bill"].values.reshape(-1, 1) y = df["tip"].values regr = linear_model.LinearRegression() regr.fit(X, y) df["predicted_tip"] = regr.predict(X) df["residual"] = df["tip"] - df["predicted_tip"] residual_mean, residual_std = df["residual"].mean(), df["residual"].std() df["residual_normalized"] = (((df["tip"] - df["predicted_tip"]) - residual_mean) / residual_std).abs() ## determine outliers using whatever method you like outliers = df.loc[df["residual_normalized"] > 3.0, ["total_bill","tip"]] fig = px.scatter(df, x="total_bill", y="tip", trendline="ols", trendline_color_override="red") ## add text to outliers using their (x,y) coordinates: for x,y in outliers.itertuples(index=False): fig.add_annotation( x=x, y=y, text="outlier", showarrow=False, yshift=10 ) fig.show()
Question: I know the following about various python HTTP libraries:  Requests does not support HTTP/2 requests. Hyper does support HTTP/2 requests, but is archived as of early 2021 and wouldn't be a good choice for new projects. HTTPX does support HTTP/2, but this support is optional, requires installing extra dependencies, and comes with some caveats about rough edges. AIOHTTP does not support HTTP2 yet (as of mid April 2022).  The focus of this project is also not solely on being a client -- this package also includes a server.    The other major HTTP request library I'm aware of is urllib3. This is what OpenAPI Generator uses by default when generating python client libraries. My Questions are: Can urrlib3 be configured to make HTTP/2 requests? I cannot find any information on http2 support in the documentation, and through my testing of a generated OpenAPI client, all requests are HTTP/1.1. If the answer is no currently, are the maintainers planning HTTP/2 support? I cannot find any evidence of this in the project's open issues.

Answer: I asked about this in the urllib3 discord, and got an answer from one of the maintainers that corroborates what Tim Roberts commented;  Proper HTTP/2 implementations require async/await to take advantage of the main different feature in HTTP/2, which is making requests in parallel. urllib3 in particular is not planning to support this because it'll in general require a rewrite.
Question: Variable 'a' could be of type - int/float/decimal.Decimal (but not a string) I want to check if its a decimal.Decimal type. Following works: import decimal a = decimal.Decimal(4) if type(a) is decimal.Decimal: print('yes decimal') else: print('not decimal')  But, is there a righter way of doing the same? tnx.

Answer: Use isinstance which outputs True or False. result = isinstance(a, decimal.Decimal) print(result)
Question: I have a dictionary: matches = {282: 285, 266: 277, 276: 293, 263: 264, 286: 280, 356: 1371, 373: 262, 314: 327, 294: 290, 285: 282, 277: 266, 293: 276, 264: 263, 280: 286, 1371: 356, 262: 373, 327: 314, 290: 294}  And a df, like so:  team_id 0 327 1 293 2 373 3 282 4 314 5 263 6 280 7 354 8 264 9 294 10 1371 11 262 12 266 13 356 14 290 15 285 16 286 17 275 18 277 19 276   Now I'm trying to create an 'adversary_id' column, mapped from the dict, like so: df['adversary_id'] = df['team_id'].map(matches)  But this new column adversary_id is being converted to type float, and two rows are ending up with NaN: Why, if all data is type int? How do I fix this?

Answer: This is because the np.nan or NaN (they are not exact same) values you see in the dataframe are of type float. It is a limitation that pitifully can't be avoided as long as you have NaN values in your code. Kindly read more in pandas' documentation here.  Because NaN is a float, a column of integers with even one missing values is cast to floating-point dtype (see Support for integer NA for more). pandas provides a nullable integer array, which can be used by explicitly requesting the dtype:  The proposed solution is to force the type with: df['team_id'] = pd.Series(df['team_id'],dtype=pd.Int64Dtype())  Returning: <class 'pandas.core.frame.DataFrame'> RangeIndex: 5 entries, 0 to 4 Data columns (total 1 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Example 4 non-null Int64 dtypes: Int64(1) memory usage: 173.0 bytes
Question: I'm trying to get an application with a React/NodeJS frontend and a Flask backend to run locally for development purposes. I've been scouring StackOverflow for the last hour, but I can't seem to get past the CORS-issue. I have: import json from flask import Flask, request from flask_cors import CORS, cross_origin app = Flask(__name__) # Enable cors requests CORS(app) # Initiate model @app.route("/query", methods=["POST"]) @cross_origin(origin="*", headers=["Content-Type"]) def query(): """Endpoint for receiving bot response""" print(request) query = request.json bot_answer = blablagetanswer() ... #filling the json return json.dumps({"botResponse": bot_answer}) if __name__ == "__main__": app.run( host="0.0.0.0", port=80, debug=True, )  I have read multiple answers, and tried many variations of CORS handling in Flask (tried with and without the @cross_origin() decorater, added @cross_origin(origin="*") or even @cross_origin(origin="*", headers=["Content-Type"]) right under the @app.route()), but to no avail. I even tried adding these two lines to my flask app: app.config["CORS_HEADERS"] = "Content-Type" CORS(app, resources={r"/*": {"origins": "*"}})  It also didn't help. I'm fetching from React like this: var jsonData = { "lastConversations": [this.state.chatInput] } console.log("Sending Chat: " + JSON.stringify(jsonData, null, 2)); fetch('http://my-ip:80/query', { method: 'POST', mode: 'cors', body: JSON.stringify(jsonData) })  and the log does contain the text I want to send, but Chrome just says:  Access to fetch at 'http://my-ip/query' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.  What am I missing? This is just for local testing, how can I get it to work?

Answer: Turns out when working with JSON, it's important to make sure you let the other side know that you're sending application/json data. Modifying the fetch like this solved it: var jsonData = { "lastConversations": [this.state.chatInput] } console.log("Sending Chat: " + JSON.stringify(jsonData, null, 2)); fetch('http://my-ip:80/query', { method: 'POST', mode: 'cors', headers: { # this is the key part 'Content-Type': 'application/json' }, body: JSON.stringify(jsonData) })
Question: I want to add type hints for arguments in functions that accept any google protocol buffer object. def do_something(protobuf_obj: WHAT_IS_HERE): # protobuf_obj can be any protocol buffer instance pass  What class should I put there from the google.protobuf library?

Answer: I ended up using the Message abstract base class. From the docs:  class google.protobuf.message.Message Abstract base class for protocol messages. Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.  So, now it looks like: from google.protobuf.message import Message def do_something(protobuf_obj: Message): # protobuf_obj can be any protocol buffer instance pass
Question: I have written a Python Dash Application and it works completely fine on my local computer. Now, I want to be able to deploy this application on a server within the corporate network. I do NOT want to deploy this on Heroku etc because the datasource is an internal API. How do I go about deploying this application on the server? It's a Linux based machine. I found this post that says use the code below but not quite sure where to add this piece of code. waitress-serve --host=0.0.0.0 --port=8080 appname:app.server 

Answer: The code you are referring to, waitress-serve, is a command-line wrapper bound to the function waitress.serve provided by Waitress. You run it in your terminal or from a shell script.  Waitress is a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 3.7+.  You can install it with pip install waitress. @see waitress-serve documentation here.
Question: I create 2 figures. One is a Seaborn figure and one is a matplotlib figure. Now I would like to combined those 2 figures into 1 combined figure. While the matplotlib figure is being displayed on the left hand side, the seaborn figure is not displayed. Here is the code #Plot the seaborn figure fig, ax = plt.subplots(figsize=(12,6)) sns.kdeplot(data=data_train.squeeze(), color='cornflowerblue', label='train', fill=False, ax=ax) sns.kdeplot(data=data_valid.squeeze(), color='orange', label='valid', fill=False, ax=ax) sns.kdeplot(data=data_test.squeeze(), color='green', label='test', fill=False, ax=ax) ax.legend(loc=2, prop={'size': 20}) plt.tight_layout() plt.xticks(fontsize =20) plt.yticks(fontsize =20) plt.title(f"{currentFeature}\nKernel density functions", fontsize = 20) plt.ylabel('Density',fontsize=20) plt.show() # Plot a matplotlib figure in a combined plot on the left side X1 = np.linspace(data_train.min(), data_train.max(), 1000) X2 = np.linspace(data_valid.min(), data_valid.max(), 1000) X3 = np.linspace(data_test.min(), data_test.max(), 1000) fig, ax = plt.subplots(1,2, figsize=(12,6)) ax[0].plot(X1, histogram_dist_train.pdf(X1), label='train') ax[0].plot(X2, histogram_dist_valid.pdf(X2), label='valid') ax[0].plot(X3, histogram_dist_test.pdf(X3), label='test') ax[0].set_title('matplotlib figure', fontsize = 14) ax[0].legend() #Try to plot the same seaborn figure from above on the right side of the combined figure ax[1].plot(sns.kdeplot(data=data_train.squeeze(), color='cornflowerblue', label='train', fill=False, ax=ax)) ax[1].plot(sns.kdeplot(data=data_valid.squeeze(), color='orange', label='valid', fill=False, ax=ax)) ax[1].plot(sns.kdeplot(data=data_test.squeeze(), color='green', label='test', fill=False, ax=ax)) ax[1].set_title('seaborn figure', fontsize = 14) ax[1].legend()  When running the code I get the following error "AttributeError: 'numpy.ndarray' object has no attribute 'xaxis'". The single seaborn figure is created and the combined matplotlib figure is also created. But only on the left side you can see the correct matplotlib figure while on the right side it is just empty. Any ideas how I can do this?

Answer: Some hopefully clarifying comments: A figure is the top-level container for all plot elements. It's misleading/incorrect to refer to a matplotlib figure or seaborn figure, when really you're referring to an Axes. This creates one figure with two subplots. fig, ax = plt.subplots(1,2, figsize=(12,6))  Pure matplotlib plotting: ax[0].plot(X1, histogram_dist_train.pdf(X1), label='train'))  Seaborn plotting: passing an existing Axes to kdeplot: sns.kdeplot(data=data_train.squeeze(), color='cornflowerblue', label='train', fill=False, ax=ax[1])
Question: I am plotting a city boundary (geopandas dataframe) to which I added a basemap using contextily. I would like to apply opacity to the region of the map outside of the city limits. The below example shows the opposite of the desired effect, as the opacity should be applied everywhere except whithin the city limits. import osmnx as ox import geopandas as gpd import contextily as cx berlin = ox.geocode_to_gdf('Berlin,Germany') fig, ax = plt.subplots(1, 1, figsize=(10,10)) _ = ax.axis('off') berlin.plot(ax=ax, color='white', edgecolor='black', alpha=.7, ) # basemap cx.add_basemap(ax,crs=berlin.crs,) plt.savefig('stackoverflow_question.png', dpi=100, bbox_inches='tight', )  Plot showing opposite of desired result: 

Answer: You can create a new polygon that is a buffer on the total bounds of your geometry minus your geometry import osmnx as ox import geopandas as gpd import contextily as cx import matplotlib.pyplot as plt from shapely.geometry import box berlin = ox.geocode_to_gdf("Berlin,Germany") notberlin = gpd.GeoSeries( [ box(*box(*berlin.total_bounds).buffer(0.1).bounds).difference( berlin["geometry"].values[0] ) ], crs=berlin.crs, ) fig, ax = plt.subplots(1, 1, figsize=(10, 10)) _ = ax.axis("off") notberlin.plot( ax=ax, color="white", edgecolor="black", alpha=0.7, ) # basemap cx.add_basemap( ax, crs=berlin.crs, ) # plt.savefig('stackoverflow_question.png', # dpi=100, # bbox_inches='tight', # )
Question: I am new to using Geopandas and plotting maps from Geo Dataframe. I have two Geo DataFrames which belong to the same city. But they are sourced from different sources. One contains the Geometry data for houses and another for Census tracts. I want to plot the houses' boundary on top of the tract boundry. Below is the first row from each data set. I am also not sure why the Geometry Polygon values are on such a different scale in each of these datasets.  Houses Data Set House Data Tract Data Set Tract Data  I tried the following code in the Jupyer Notebook but nothing is showing up. f, ax = plt.subplots() tract_data.plot(ax=ax) house_data.plot(ax=ax)  But an empty plot shows up. This is my first post. Please let me know what else I can provide.

Answer: You probably need to set the correct coordinate reference system (crs). More info here An easy fix might be f, ax = plt.subplots() tract_data.to_crs(house_data.crs).plot(ax=ax) house_data.plot(ax=ax)
Question: I'm trying to convert some python pandas into polars. I'm stuck trying to convert pandas pivot_table function into polars. The following is the working pandas code. I can't seem to get the same behavior with the Polars pivot function. The polars pivot function forces the column parameter and uses the column values as headers instead of the column label as a header. I'm going for the same output below but with Polars instead of Pandas. df = pd.DataFrame({"obj" : ["ring", "shoe", "ring"], "price":["65", "42", "65"], "value":["53", "55", "54"], "date":["2022-02-07", "2022-01-07", "2022-03-07"]}) table = pd.pivot_table(df, values=['price','value','date'],index=['obj'], aggfunc={'price': pd.Series.nunique,'value':pd.Series.nunique,'date':pd.Series.nunique}) print(table)  Outputs the following:  date price value obj ring 2 1 2 shoe 1 1 1 

Answer: In Polars, we would not use a pivot table for this. Instead, we would use the group_by and agg functions. Using your data, it would be: import polars as pl df = pl.from_pandas(df) df.group_by("obj").agg(pl.all().n_unique())  shape: (2, 4) ┌──────┬───────┬───────┬──────┐ │ obj ┆ price ┆ value ┆ date │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ u32 ┆ u32 ┆ u32 │ ╞══════╪═══════╪═══════╪══════╡ │ ring ┆ 1 ┆ 2 ┆ 2 │ │ shoe ┆ 1 ┆ 1 ┆ 1 │ └──────┴───────┴───────┴──────┘  pivot and unpivot Where we would use the pivot function in Polars is to summarize a dataset in 'long' format to a dataset in 'wide' format. As an example, let's convert your original dataset to 'long' format using the unpivot function. df2 = df.unpivot(index="obj") print(df2)  shape: (9, 3) ┌──────┬──────────┬────────────┐ │ obj ┆ variable ┆ value │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ str │ ╞══════╪══════════╪════════════╡ │ ring ┆ price ┆ 65 │ │ shoe ┆ price ┆ 42 │ │ ring ┆ price ┆ 65 │ │ ring ┆ value ┆ 53 │ │ shoe ┆ value ┆ 55 │ │ ring ┆ value ┆ 54 │ │ ring ┆ date ┆ 2022-02-07 │ │ shoe ┆ date ┆ 2022-01-07 │ │ ring ┆ date ┆ 2022-03-07 │ └──────┴──────────┴────────────┘  Now let's use pivot to summarize this 'long' format dataset back to one in "wide" format and simply count the number of values. df2.pivot(on='variable', index='obj', aggregate_function=pl.len())  shape: (2, 4) ┌──────┬──────┬───────┬───────┐ │ obj ┆ date ┆ price ┆ value │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ u32 ┆ u32 ┆ u32 │ ╞══════╪══════╪═══════╪═══════╡ │ ring ┆ 2 ┆ 2 ┆ 2 │ │ shoe ┆ 1 ┆ 1 ┆ 1 │ └──────┴──────┴───────┴───────┘  Does this help clarify the use of the pivot functionality?
Question: I want to fill the null values of a column with the content of another column of the same row in a lazy data frame in Polars. Is this possible with reasonable performance?

Answer: There's a function for this: fill_null. Let's say we have this data: import polars as pl df = pl.DataFrame({'a': [1, None, 3, 4], 'b': [10, 20, 30, 40] }).lazy() print(df.collect())  shape: (4, 2) ┌──────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞══════╪═════╡ │ 1 ┆ 10 │ │ null ┆ 20 │ │ 3 ┆ 30 │ │ 4 ┆ 40 │ └──────┴─────┘  We can fill the null values in column a with values in column b: df.with_columns(pl.col('a').fill_null(pl.col('b'))).collect()  shape: (4, 2) ┌─────┬─────┐ │ a ┆ b │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 1 ┆ 10 │ │ 20 ┆ 20 │ │ 3 ┆ 30 │ │ 4 ┆ 40 │ └─────┴─────┘  The performance of this will be quite good.
Question: How can I achieve the equivalents of SQL's IN and NOT IN? I have a list with the required values. Here's the scenario: import pandas as pd import polars as pl exclude_fruit = ["apple", "orange"] df = pl.DataFrame( { "A": [1, 2, 3, 4, 5, 6], "fruits": ["banana", "banana", "apple", "apple", "banana", "orange"], "B": [5, 4, 3, 2, 1, 6], "cars": ["beetle", "audi", "beetle", "beetle", "beetle", "frog"], "optional": [28, 300, None, 2, -30, 949], } ) df.filter(~pl.select("fruits").str.contains(exclude_fruit)) df.filter(~pl.select("fruits").to_pandas().isin(exclude_fruit)) df.filter(~pl.select("fruits").isin(exclude_fruit)) 

Answer: You were close. df.filter(~pl.col('fruits').is_in(exclude_fruit))  shape: (3, 5) ┌─────┬────────┬─────┬────────┬──────────┐ │ A ┆ fruits ┆ B ┆ cars ┆ optional │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ str ┆ i64 ┆ str ┆ i64 │ ╞═════╪════════╪═════╪════════╪══════════╡ │ 1 ┆ banana ┆ 5 ┆ beetle ┆ 28 │ │ 2 ┆ banana ┆ 4 ┆ audi ┆ 300 │ │ 5 ┆ banana ┆ 1 ┆ beetle ┆ -30 │ └─────┴────────┴─────┴────────┴──────────┘
Question: Does typing.TypedDict allow extra keys? Does a value pass the typechecker, if it has keys which are not present on the definition of the TypedDict?

Answer: It depends. PEP-589, the specification of TypedDict, explicitely forbids extra keys:  Extra keys included in TypedDict object construction should also be caught. In this example, the director key is not defined in Movie and is expected to generate an error from a type checker: m: Movie = dict( name='Alien', year=1979, director='Ridley Scott') # error: Unexpected key 'director'   [emphasis by me] The typecheckers mypy, pyre, and pyright implement this according to the specification. However, it is possible that a value with extra keys is accepted. This is because subtyping of TypedDicts is allowed, and the subtype might implement the extra key. PEP-589 only forbids extra keys in object construction, i.e. in literal assignment. As any value that complies with a subtype is always deemed to comply with the parent type and can be upcasted from the subtype to the parent type, an extra key can be introduced through a subtype: from typing import TypedDict class Movie(TypedDict): name: str year: int class MovieWithDirector(Movie): director: str # This is illegal: movie: Movie = { 'name': 'Ash is purest white', 'year': 2018, 'director': 'Jia Zhangke', } # This is legal: movie_with_director: MovieWithDirector = { 'name': 'Ash is purest white', 'year': 2018, 'director': 'Jia Zhangke', } # This is legal, MovieWithDirector is a subtype of Movie movie: Movie = movie_with_director  In the example above, we see that the same value can sometimes be considered complying with Movie by the typing system, and sometimes not. As a consequence of subtyping, typing a parameter as a certain TypedDict is not a safeguard against extra keys, because they could have been introduced through a subtype. If your code is sensitive with regard to the presence of extra keys (for instance, if it makes use of param.keys(), param.values() or len(param) on the TypedDict parameter param), this could lead to problems when extra keys are present. A solution to this problem is to either handle the exceptional case that extra keys are actually present on the parameter or to make your code insensitive against extra keys. If you want to test that your code is robust against extra keys, you cannot simply add a key in the test value: def some_movie_function(movie: Movie): # ... def test_some_movie_function(): # this will not be accepted by the type checker: result = some_movie_function({ 'name': 'Ash is purest white', 'year': 2018, 'director': 'Jia Zhangke', 'genre': 'drama', })  Workarounds are to either make the type checkers ignore the line or to create a subtype for your test, introducing the extra keys only for your test: class ExtendedMovie(Movie): director: str genre: str def test_some_movie_function(): extended_movie: ExtendedMovie = { 'name': 'Ash is purest white', 'year': 2018, 'director': 'Jia Zhangke', 'genre': 'drama', } result = some_movie_function(test_some_movie_function) # run assertions against result